-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem saving the same (in Java) object multiple times
PostPosted: Mon Apr 06, 2009 1:21 pm 
Newbie

Joined: Mon Apr 06, 2009 12:59 pm
Posts: 1
I tried to search for an answer to this problem to no avail, so I will post my problem.

Domain object:
Person, Education and School; each maps to a table in the DB. An education is defined as an object containing a school, a start date and an end date. A person can have multiple educations (i.e. have gone to the same school but at different times and receiving different degrees). I have an unique constraint on the school name as I don't want the same school appear multiple times in the database.

Problem:
During the import process, I would loop through the education for a person and create the school if it's not already existed in the DB. The problem arises when the person goes to the same school multiple times and the school was not previously existed in the DB.

Example:
Code:
Person A -> Education 1 -> School A
                        -> Start Date A
                        -> End Date B

         -> Education 2 -> School A
                        -> Start Date C
                        -> End Date D


I am doing a big save at the end of each person. When I try to persist PersonA, I get the unique constraint violation on school name. I understand why it's doing it: when the code does a lookup in the DB to check to see if SchoolA exists in the DB, it's not at that point. That's why it created 2 instances of Java School object containing the same data. When Hibernate tries to persist SchoolA in Education2, SchoolA already exists in the database. What I don't get is how come hibernate doesn't detect SchoolA already exists when trying to save for the second time. Any help would be appreciated.


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

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.