• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

hibernate table creation

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post and UseCodeTags. Also is there a question here? Did you mean to post this in this thread?
 
Kavitha hebbar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I finally got it.In the employee.hbm.xml,I removed entity name.Then i got it.
Thank you.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic