Forums Register Login

hibernate table creation

+Pie Number of slices to send: Send
I have two beans namely employee and department.This is one-to-many relationship where in one department many employees are working.department_id is the primary key.


This is Runner class which has main method

package Manytoone;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Runner {

static void main(String[] args) {
SessionFactory factory=FactoryOneInstance.getInstance();
Session session=factory.openSession();
Transaction trans=session.beginTransaction();
Employee employee=new Employee();
Department dept=new Department();
employee.setName("Kavitha");
dept.setName("Software");
dept.setEmployee(employee);

try
{
trans.begin();
session.save("dept",dept);
trans.commit();
}
catch(HibernateException e)
{
e.printStackTrace();
trans.rollback();
}

}

}

This is employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="Manytoone">
<class name="Employee" table="employee" entity-name="emp">
<id name="empid" column="Id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
This is department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Manytoone.Department" table="department" entity-name="dept">
<id name="department_id" column="deptid">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<many-to-one name="employee" class="Manytoone.Employee" cascade="all" >
<column name="empid"></column>
</many-to-one>
</class>

</hibernate-mapping>
1
+Pie Number of slices to send: Send
Please edit your post and UseCodeTags. Also is there a question here? Did you mean to post this in this thread?
+Pie Number of slices to send: Send
I have two beans namely employee and department.This is one-to-many relationship where in one department many employees are working.department_id is the primary key.
when i run this it asks "select a java application" giving some options like SchemaUpdate,DBdialect....If i choice any one it shows so many exceptions.Wher i have gone wrong?One difference is in configuration file i have given <property name="hbm2ddl.auto">create</property>

This is Runner class which has main method


I'm right clicking on runner class and run as java application only.still it is coming like that.
Please help me.
Thanks
+Pie Number of slices to send: Send
What does your hibernate.cfg.xml look like? How about your FactoryOneInstance class? Are you right clicking the runner class and saying run as java application? Why are you beginning a transaction twice and never committing it or closing the session?

Try checking out the tutorials
http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/files/hibernate-tutorials.zip

and have a look at the getting started guide
http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/
+Pie Number of slices to send: Send
Hi,
I finally got it.In the employee.hbm.xml,I removed entity name.Then i got it.
Thank you.
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1450 times.
Similar Threads
Session onject throwing exception when trying to save a object or begin a transaction
How to configure xxx.hbm.xml
Hibernate 3.0 : Where am I going wrong?
hybernate doubt
problem saving with foreign key error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:16:58.