• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cascade attribute's value

 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using Hibernate and I have one requirement in my project which is that I need to set value of Cascade to "none" when I save the object to the database and I need to set the value of this attribute to "All" when I load the object from the database for the same class. If I do this then I am able to avoid the duplications in the database. So could anybody tell me how to achieve this functionality for Cascade?
[ July 14, 2008: Message edited by: Nitin Kasat ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I know requirements are requirements, but WHY?

My issue here is that it seems that there is an issue with duplicate records that you are trying to get ORM to solve for you, rather than at the root cause of the problem.

You are trying making ORM be responsible for something it shouldn't be responsible for, the application sending duplicate records for Hibernate to insert. I am thinking in the application code is what should be responsible for the duplication issue.

Mark
 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
I will tell you the details about mjy code.

I have two classes:
public class A{
private int id;
private B b;

//and getters and setters for all attributes.

}

public class B{
int id;
//Other attributes and all getter setters
}

Now there are two tables for Class suppose AT and for class B BT.
Table BT contains the static data suppose 8 rows which do not change. Now in table AT for attribute a I want the reference i.e. foreign key from Table BT. Hibernate will do this association using mapping file.

Now consider the following senario:

I create the instance of Class A and saveOrUpdate it using session.saveOrUpdate() method. If the cascades value is "all" then when saving this new instance it will create new id's for Class A as well as B and associate the classes A and B using new ids. So even if the values of columns other than id don't change in Class B the hibernate is going to save this as new instance in the table BT. So this creates the duplications of rows in the database. To avoid this I want the value of cascade ="none" when I will save the instance and it should be "all" when I load the instance of class A so that all associations will be taken care by hibernate.

I think you have understood my problem. If you think I am wrong somewhere with hibernate's persistence logic please clarify me.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the only way Hibernate will do an insert statement into table B, is if the object B in your Collection that A has does not have an id assigned. It object B already has an id set, then Hibernate won't do an insert statement on it.

Mark
 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So Hibernate will takecare of avoiding duplication. That is it will generate the id only if it is not able to find the same object in the database and if it finds then will assign the existing id. Am I right what you mean to say? Thanks for your reply.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitin Kasat:
Ok. So Hibernate will takecare of avoiding duplication. That is it will generate the id only if it is not able to find the same object in the database and if it finds then will assign the existing id. Am I right what you mean to say? Thanks for your reply.


Not exactly. When you call saveOrUpdate, hibernate will look at the B object and call getId. If it does not return a value, hibernate will assume it needs to do an insert. So make sure the B object has an id already. How to do that, load your B objects from the database is one way. Use a second level cache to cache the B objects is another way.

Mark
 
Nitin Kasat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it. Thanks very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic