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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate's equivalent to TopLink's registerObject()
PostPosted: Mon Feb 27, 2006 12:26 pm 
Newbie

Joined: Mon Feb 27, 2006 12:02 pm
Posts: 8
Hello.

I am migrating one of my project's code from TopLink to Hibernate. Currently I have problem finding replacement to TopLink's UnitOfWork.registerObject() method.

UnitOfWork.registerObject() is used to register a transient object to the session and the object will be saved to database at commit time. All modification to the object after calling this method and before commit the transaction will also be included into single SQL insert.

Code example - DAO
Code:
public Employee createEmployee(Object txHandle, String employeeCode) {
    UnitOfWork uow = (UnitOfWork) txHandle;

    Employee emp = new Employee();
    emp.setCode(code);

    uow.registerObject(emp);

    return emp;
}


Code example - Caller
Code:
... Begin transaction

Employee emp = employeeDAO.createEmployee(session, "code");
emp.setName("name");

... Commit transaction


Above code results in a single SQL insert with both code and name fields set.

If I modify the DAO to use Session.save(), there will be SQL insert followed by another SQL update at commit time which cause the firsts SQL insert statement to fail since some of mandatory field are not set.

Is there any equivalent API in Hibernate to just register a transient object with the session and include all modification after that into a single SQL insert at commit time ? If there is, it will be very nice. If not, I have to also change the coding style of existing code in business tier (instead of haiving to modify oly DAO tier) which I prefer not to.

Any comment is very welcome. Thank you very much in advanced.

Best regards,


Top
 Profile  
 
 Post subject: merge
PostPosted: Tue Feb 28, 2006 6:02 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Session.merge()
http://www.hibernate.org/hib_docs/v3/re ... tices.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 7:33 am 
Newbie

Joined: Mon Feb 27, 2006 12:02 pm
Posts: 8
Hi.

Thank you very much for you comment. I have tries it out but without success.

I tried by writing a simple code
Code:
Transaction tx = session.beginTransaction();

Employee e = new Employee();
e.setCode("A0001");
e = (Employee) session.merge(e);

// Both "code" and "name" are required fields in database
e.setName("NAME");

tx.commit()

After the transaction is committed, two SQL are generated. The first one being INSERT and second one being UPDATE whcih cause the first INSERT statement to fail since the mandatory "name" field is not set. While what I would like is to get only one INSERT SQL at the end with both "code" and "name" fields set.


Top
 Profile  
 
 Post subject: flow
PostPosted: Thu Mar 02, 2006 11:33 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
If you need to set few properties than do just that:

Transaction tx = session.beginTransaction();
Employee e = session.get(Employee.class, id)
e.setName("NAME");
tx.commit()

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 5:43 am 
Newbie

Joined: Mon Feb 27, 2006 12:02 pm
Posts: 8
Thank you again for your comment, but it is still not what I need.

What I have to do is to convert my existing DAO code implemented with TopLink to Hibernate. The problem is because there are some different in their programming model.

In TopLink, there is a method, registerObject() which register a transient with TopLink session. At the time this method is called, it is not required that all required properties of the object has to be set. Its properties can be set after the object is registed and TopLink will just issue a single INSERT statement at transaction commit time.

For example, existing TopLink code will look like this
Code:
// In DAO
public Employee createEmployee(String code) {
  Employee e = new Employee();
  e.setCode(code);

  session.registerObject(e);       // TopLink API
  return e;
}

// Later in caller code
.. Begin transaction

Employee e = employeeDAO.createEmployee("CODE");
e.setName("NAME");

.. commit transaction

At commit time, only one INSERT statement is generated to insert both code and name property.

If I convert above code to following Hibernate code
Code:
// In DAO
public Employee createEmployee(String code) {
  Employee e = new Employee();
  e.setCode(code);

  session.save(e);     // Hibernate API
  return e;
}

// Later in caller code
.. Begin transaction

Employee e = employeeDAO.createEmployee("CODE");
e.setName("NAME");

.. commit transaction

This code generated two SQL statement. First one is INSERT caused by Session.save() method which only insert code column. The second statement is UPDATE the name column which is generated by Transaction.commit() method. The first INSERT record will fail because it only set code column while both code and name columns are mandatory.

What I am trying to find is the equivalent to TopLink registerObject method which does not cause the INSERT statement to insert just the properties which are set at the time is method is called, but rather generate INSERT command with all properties set at the time transaction is commit.

But if there is no method in Hibernate with this same behaviour, I will have to change my existing DAO code to populate all mandatory properties before calling Session.save().

Best regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.