Problem in persisting simple object in Hibernate
I have the following Model class:
package com.swaranga.model;
public final class Book
{
private Long id;
private String title;
private String isbn;
public Book(){}
public Book(String title, String isbn)
{
this.title = title;
this.isbn = isbn;
}
public final Long getId(){
return id;
}
private final void setId(Long id)
{
this.id = id;
}
public final String getTitle()
{
return title;
}
public final void setTitle(String title)
{
this.title = title;
}
public final String getIsbn()
{
return isbn;开发者_开发知识库
}
public final void setIsbn(String isbn)
{
this.isbn = isbn;
}
//assume valid equals and hashcode
}
The have the following mapping file Book.hbm.xml:
<hibernate-mapping>
<class name="com.swaranga.model.Book" table="book">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="title" column="title" type="string"/>
<property name="isbn" column="isbn" type="string"/>
</class>
</hibernate-mapping>
The following database schema:
CREATE TABLE `hibernatetest`.`book` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(45) NOT NULL,
`isbn` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The following hibernate config file:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest </property>
<property name="connection.username"> root </property>
<property name="connection.password"> latitude </property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="myeclipse.connection.profile">mysql</property>
<mapping resource="com\swaranga\model\Book.hbm.xml" />
</session-factory>
</hibernate-configuration>
And finally the following code to persist a Book
object:
public class Main
{
public static void main(String[] args)
{
File f = new File("hibernate.cfg.xml");
Configuration cfg = new Configuration();
SessionFactory sessionFactory = cfg.configure(f).buildSessionFactory();
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(new Book("JDBC", "ISBN_!@#"));
s.flush();
s.disconnect();
}
}
I am getting the following output in the console:
Hibernate: insert into book (title, isbn) values (?, ?)
But when I check my database, there are no entries.
Apologies for such a long question, but I felt necessary to supply all the details. Please help. Thanks in advance.
Try editing the end of your main method to this:
Transaction tx = s.beginTransaction();
s.save(new Book("JDBC", "ISBN_!@#"));
tx.commit();
s.flush();
s.disconnect();
Is "auto" a valid generator class?
It is not listed on the O/R mapping part of the docs and from what it looks like you are doing you are not generating an ID in code or in the database:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html
I would look at native if you want to assign ids, or do an identity generator to let MySQL handle it.
Have you tried id type "long"?
You have not set the id of the database to be auto-increment. Try that. Should work.
精彩评论