Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hibernate Issue.No error but values doent get inserted into table

843859Sep 25 2007 — edited Sep 25 2007
Hi,
Im a beginner in Hibernate.I have gone through a sample code in net and tried executed it but the values doesnt get inserted into table
Here is the code:
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">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:@192.168.1.100:1521:modula
</property>
<property name="hibernate.connection.username">
developer
</property>
<property name="hibernate.connection.password">developer</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="emp2.hbm.xml" />
</session-factory>
</hibernate-configuration>

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

<hibernate-mapping>
<class name="roseindia.tutorial.hibernate.Emp2" table="EMP2">
<id name="empNo" type="string" column="EMPNO" >
<generator class="assigned"/>
</id>

<property name="empName">
<column name="EMPNAME" />
</property>

</class>
</hibernate-mapping>

Emp2 is the table name and Empno and Empname are the columns where both are varchar and empno is not null

Here is the class that im calling
public class FirstExample {
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();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Emp2 contact = new Emp2();
contact.setEmpName("Sarada");
contact.setEmpNo("321");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}

Emp2.java
public class Emp2 {
private String empName;
private String empNo;
// private String email;
// private long id;

/**
* @return Name
*/
public String getEmpName() {
return empName;
}

/**
* @return First Name
*/
public String getEmpNo() {
return empNo;
}



/**
* @param string Sets the Name
*/
public void setEmpName(String string) {
empName = string;
System.out.println("empName:"+empName);
}

/**
* @param string Sets the First Name
*/
public void setEmpNo(String string) {
empNo = string;
System.out.println("emp no:"+empNo);
}


}

Please help me out in understanding the problem

Thanks and Regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 23 2007
Added on Sep 25 2007
2 comments
743 views