Janith Jeewantha
Janith Jeewantha

Reputation: 195

Hibernate SQL Error in generated query (Potentially a mapping mistake)

I'm making this simple app using Hibernate to do some CRUD operations. I'm having this error in the query generated by hibernate

Config XML:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/library</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.default_schema">library</property>
    <property name="hbm2ddl.auto" value="create"/>
    <!-- Mappings -->
    <mapping resource="librarysystem/mappings/Role.hbm.xml"/>
    <mapping resource="librarysystem/mappings/Librarian.hbm.xml"/>
    <mapping resource="librarysystem/mappings/Task.hbm.xml"/>
    <mapping resource="librarysystem/mappings/Library.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

POJO:

public class Library  implements java.io.Serializable {

    private int id;
    private String name;
    private Set librarians = new HashSet(0);

    //Constructors, getters and setters...
}

Mapping:

<hibernate-mapping>
    <class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="45" />
        </property>
        <set name="librarians" table="librarian" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="libraryid" not-null="true" />
            </key>
            <one-to-many class="librarysystem.entities.Librarian" />
        </set>
    </class>
</hibernate-mapping>

Code to get the list:

List<Library> libraries = session.createQuery("FROM Library").list();

When I run a query to get the list of libraries, an exception occurs saying that the query syntax is wrong

the log output of the query is:

select
    library0_.id as id1_2_,
    library0_.name as name2_2_ 
from
    library.library.library library0_

How did the library.library.library happened? Any help please?

I've given the minimum details as I thought was necessary. If you need more code to find the error (like other POJOs and mappings), please inform me

Upvotes: 2

Views: 76

Answers (1)

Old Schooled
Old Schooled

Reputation: 1272

How did the library.library.library happened? Any help please?

I'm no expert here but I think the tripple library here comes from the table being named library within a Database (catalog) named library.

<class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">

The table name and the catalog name are the same. So we are looking for a catalog called library library within which we want a table called library library.library. Although I'm not 100% on the third library, I think I'm in the right direction.

On a side note, I find it easier to work with hibernate annotation instead of xml mapping.

Upvotes: 1

Related Questions