hi
This id dhanasekar, i am new to hibernate i am using eclipse and postgre sql when i run the program the values are not inserted in to the db, i will should what it has been shown to me
Inserting Book object into database..
Hibernate: select max(id) from book
Book object persisted to the database.
Hibernate: insert into book (bookname, id) values (?, ?)
i will show u my code here:
Book.java
/**
* @author Deepak Kumar
*
*
http://www.roseindia.net * Java Class to map to the database Book table
*/
package roseindia.tutorial.hibernate;
public class Book {
private long lngBookId;
private String strBookName;
/**
* @return Returns the lngBookId.
*/
public long getLngBookId() {
return lngBookId;
}
/**
* @param lngBookId The lngBookId to set.
*/
public void setLngBookId(long lngBookId) {
this.lngBookId = lngBookId;
}
/**
* @return Returns the strBookName.
*/
public String getStrBookName() {
return strBookName;
}
/**
* @param strBookName The strBookName to set.
*/
public void setStrBookName(String strBookName) {
this.strBookName = strBookName;
}
}
IdIncrementExample.java
/**
* @author Deepak Kumar
*
*
http://www.roseindia.net * Example to show the increment class of hibernate generator element to
* automatically generate the primay key
*/
package roseindia.tutorial.hibernate;
//Hibernate Imports
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class IdIncrementExample {
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Book object into database..");
Book book = new Book();
book.setStrBookName("Hibernate Tutorial");
session.save(book);
System.out.println("Book object persisted to the database.");
tx.commit();
session.flush();
session.close();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
}
}
}
contact.hbm.xml
<class name="roseindia.tutorial.hibernate.Book" table="book">
<id name="lngBookId" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="strBookName">
<column name="bookname" />
</property>
</class>
hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432:testdb </property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">Update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
plz tell me where i have done wrong...