views:

52

answers:

2

Hi!

Right now i want to make a small java application, in order to learn the hibernate framework. But it gives me the org.hibernate.MappingException: Repeated column in mapping for entity: model.Book column: author (should be mapped with insert="false" update="false"). And if I delete the author column mapping from entities.hbm.xml, then it shows me that sql message "SELEC FROM ..." but after that, it gives me 2 exceptions:

  • org.hibernate.exception.GenericJDBCException: could not execute query
  • java.sql.SQLException: No database selected.

Can anyone help me?

hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;

<hibernate-configuration>
<session-factory>

    <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name = "hibernate.connection.url">jdbc:mysql://127.0.0.1:3306</property>
    <property name = "hibernate.connection.username">root</property>
    <property name = "hibernate.connection.password"></property>
    <property name = "hibernate.connection.pool_size">10</property>
    <property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name = "hibernate.hbm2ddl.auto">update</property>
    <property name = "show_sql">true</property>

    <mapping resource = "entities.hbm.xml"/>

</session-factory>
</hibernate-configuration>

entities.hbm.xml file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping package = "model">

   <class name = "Genre" table = "virtual bookcase.genres">
      <id name = "id" column = "idGenres" type = "long">
        <generator class = "increment"/>
      </id>   
      <property name = "name" column = "name" type = "string"/>
      <set name = "books" table = "books" cascade = "all-delete-orphan">
        <key column = "idGenres" not-null = "true" />
        <one-to-many class = "Book"/>
      </set>
   </class>

   <class name = "Book" table = "virtual bookcase.books">
        <id name = "id" column = "idBooks" type = "long">
         <generator class = "increment"/>
        </id>
        <property name = "title" column = "title" type = "string"/>
        <property name = "author" column = "author" type = "string"/>
        <property name = "publisher" column = "author" type = "string"/>
        <property name = "pages" column = "pages" type = "short"/>
        <property name = "borrowed" column = "borrowed" type = "byte"/>
        <property name = "borrowedTo" column = "borrowedTo" type = "string"/>
   </class>

</hibernate-mapping>

Java entities:

public class Genre
{
    private long id;
    private String name;
    private Set<Book> books;

    public long getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Set<Book> getBooks()
    {
        return books;
    }

    public void setBooks(Set<Book> books)
    {
        this.books = books;
    }

    @Override
    public String toString()
    {
        return name;
    }
}

getBooks() method:

public Set<Book> getBooks()
    {
        Set<Book> books = null;

        connect();

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();

        Query q = s.createQuery("FROM Book");
        books = new TreeSet<Book>(q.list());

        for (Book b : books)
            System.out.println(b);

        s.close();
        sf.close();

        disconnect();

        return books;
    }
+3  A: 

You have two properties mapped to the column author:

<property name="author" column="author" type="string"/>
<property name="publisher" column="author" type="string"/>

To solve the second error append the database name to your JDBC connection URL:

<property name="hibernate.connection.url">
    jdbc:mysql://127.0.0.1:3306/dbname
</property>

Reading through your sources further I stumbled over your getBooks()-method. You should not create a SessionFactory every time you need a Hibernate Session. The creation of the SessionFactory is too expensive (measured in time) to perform this every time. A minimum solution would be a Singleton class you can ask for the SessionFactory:

public class SessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private SessionFactoryUtil() {}

    static {
       sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    public static SessionFactory getInstance() { return sessionFactory; }

}
codescape
Ohh, silly me. Thanks, one problem is resolved. But what about the second one?
DaJackal
Added the solution for your second problem to my answer.
codescape
Thank you very much, I specified now the database name in the entities xml file, and it's working. Now I have another question: This query I wrote, it's with lazy fetching? If is not, how can I do it?
DaJackal
@DaJackal, add the `lazy` attribute to the association mapping with the desired value of `true` or `false`. And if codescape solved your problem you should accept his/her answer. http://docs.jboss.org/hibernate/stable/core/reference/en/html/performance.html#performance-fetching-lazy
matt b
Thanks for everyone for your help. @codescape, if I have multiple Hibernate Sessions, and I call everytime the getInstance() method everytime I need the sessionFactory, it's not the same thing? I could use some explanation here. Thanks.
DaJackal
A: 

Maybe you should define the database/schema name inside the server. Currently you specify only the database server.

<property name = "hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/DATABASENAME</property>
Juha Syrjälä
Thank you very much, I specified now the database name in the entities xml file, and it's working. Now I have another question: This query I wrote, it's with lazy fetching? If is not, how can I do it?
DaJackal