-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate doesn't save dependend objects
PostPosted: Sat Oct 11, 2003 12:12 pm 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
I practice using Hibernate with the example of the site http://www7b.software.ibm.com/dmdd/libr ... hogal.html

When I save a department, the employees are not saved automatically. Why not?

Used Hibernate v. 2.0.3 and MySQL 4.0.15.

Here is all the code so that you can understand it:

Code:
package com.ibm.hibernate_article;

import java.sql.SQLException;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class Test {
 
  SessionFactory sessionFactory;
 
  public Test() throws HibernateException {
    Configuration cfg = new Configuration()
      .addClass(com.ibm.hibernate_article.Department.class)
      .addClass(com.ibm.hibernate_article.Employee.class);
    sessionFactory = cfg.buildSessionFactory();
  }

  public static void main(String[] args) throws HibernateException, SQLException {
    Test test = new Test();
   
    Department department = new Department();
    department.setDepartmentID(211);
    department.setCity("Austin");
    department.setState("TX");
    department.setName("IBM Global Services");
   
    Employee manager = new Employee();
    manager.setEmployeeId(422);
    manager.setDepartment(department);
    manager.setEmail("bill.gates@yahoo.com");
    manager.setFirstName("Bill");
    manager.setLastName("Gates");

    Employee worker = new Employee();
    worker.setEmployeeId(844);
    worker.setDepartment(department);
    worker.setEmail("john.smith@yahoo.com");
    worker.setFirstName("John");
    worker.setLastName("Smith");
    worker.setManager(manager);
   
    test.save(department);

  }
 
  private void save(Object object) throws HibernateException, SQLException {
    Session session = sessionFactory.openSession();
    session.save(object);
    session.flush();
    session.connection().commit();
    session.close();
//    sessionFactory.close();
    System.out.println("Saving finished!");
  }
 
}


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

<hibernate-mapping>

<class name="com.ibm.hibernate_article.Department" table="department">

<id name="departmentID" column="DepartmentID">
<generator class="assigned"/>
</id>

<property name="city" column="City"/>
<property name="name" column="Name"/>
<property name="state" column="State"/>

</class>

</hibernate-mapping>


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

<hibernate-mapping>

<class name="com.ibm.hibernate_article.Employee" table="employee">

<id name="employeeId" column="EID">
<generator class="assigned"/>
</id>

<property name="email" column="Email"/>
<property name="firstName" column="FirstName"/>
<property name="lastName" column="LastName"/>

<many-to-one name="department" column="departmentID"/>
<many-to-one name="manager" column="managerEID"/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2003 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You say Employee is dependent upon Department, but it isn't. Its not even set up as an association; Department does not know about its employees.

If you want it to work the way you have the code right now, you need to make Department have a collection of it employees and then map that association in Hibernate using cascade="save-update" or cascade="all".

The other option is to call save on each of the Employee instances and set the department association for cascading "save-update" or "all".


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2003 3:20 pm 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
steve wrote:
You say Employee is dependent upon Department, but it isn't. Its not even set up as an association; Department does not know about its employees.

If you want it to work the way you have the code right now, you need to make Department have a collection of it employees and then map that association in Hibernate using cascade="save-update" or cascade="all".

The other option is to call save on each of the Employee instances and set the department association for cascading "save-update" or "all".


Okay, I try it.

It seems to me, that Hibernate is great stuff.

We use a very difficult persisting mechanism for our entity beans. Although we use CMP, the persisting layer is very complicated to use. Of course our project leader doesn't care, he doesn't have to fight with persistence. We have Parent Objects, Dependend Objects, Composition Objects, CMR-Relations and Component Relations, Mangers, Providers and Factories for each Business Object. One needs to know a lot of things in order to persist something. And the persistence code is not easy to read because of this. You won't believe how complicated it is compared to Hibernate. I wonder how consultants get a lot of money for consulting us in such a bad way.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.