Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes hibernate table creation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "hibernate table creation" Watch "hibernate table creation" New topic
Author

hibernate table creation

Kavitha hebbar
Greenhorn

Joined: Sep 12, 2012
Posts: 9
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>
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1282

Please edit your post and UseCodeTags. Also is there a question here? Did you mean to post this in this thread?


[How To Ask Questions][Read before you PM me]
Kavitha hebbar
Greenhorn

Joined: Sep 12, 2012
Posts: 9
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
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1282

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/
Kavitha hebbar
Greenhorn

Joined: Sep 12, 2012
Posts: 9
Hi,
I finally got it.In the employee.hbm.xml,I removed entity name.Then i got it.
Thank you.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: hibernate table creation
 
Similar Threads
Hibernate 3.0 : Where am I going wrong?
How to configure xxx.hbm.xml
Session onject throwing exception when trying to save a object or begin a transaction
problem saving with foreign key error
hybernate doubt