-->
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: Sharing objects
PostPosted: Thu May 27, 2010 12:01 pm 
Newbie

Joined: Thu May 27, 2010 10:48 am
Posts: 10
Hello folks, I have the follow objects:

class Contry {
...
}

class Address {
private Country country;

@OneToOne(cascade = CascadeType.ALL)
public Country getCountry() {
return country;
}

public void setCountry(Country country) {
this.country = country;
}
}

So, I run some processes that put some information in the data base, let's say
Country country = new Country("Japan");
session.save(country);
t.commit();

after that I want to put some addresses in the data base
Address address = new Address();
* Here is the thing, I don't wanna replicate the country object
Country country = getCountryFromDataBase("Japan");
address.setCountry(country)
session.save(address)
t.commit();

I received this error:

a different object with the same identifier value was already associated with the session: [Country#3]

Any clue?


Top
 Profile  
 
 Post subject: Re: Sharing objects
PostPosted: Thu May 27, 2010 12:56 pm 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
try session.flush() or session.evict(object)


Top
 Profile  
 
 Post subject: Re: Sharing objects
PostPosted: Fri May 28, 2010 2:44 pm 
Newbie

Joined: Thu May 27, 2010 10:48 am
Posts: 10
Thanks for reply me, I could solve the problem, I closed all my sessions after I use it. But even then, I still got some errors, something like "the session is closed or terminated", I got this error because I was manipulating objects in lazy mode (I din't even know that I was in lazy mode). I modified my @OneToMany(cascade = CascadeType.ALL) annotation to @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER). About those methods, sesseion.flush() and session.evict(object), they work just fine, but I could figure out that I had 2 sessions, I was trying to remove the objects in the second one, when the problem was the first 1(I could not rich the first session because my method creates another instance of session, so the object was lost). I have some other issues, but I'll create a new post. Thanks folk.


Top
 Profile  
 
 Post subject: Re: Sharing objects
PostPosted: Fri May 28, 2010 3:06 pm 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
Just a suggestion, instead of opening a new session every time, you can use sessionFactory.getCurrentSession() and create a new transaction for each query.


Top
 Profile  
 
 Post subject: Re: Sharing objects
PostPosted: Fri May 28, 2010 3:20 pm 
Newbie

Joined: Thu May 27, 2010 10:48 am
Posts: 10
Thanks for the suggestion, getting an existent object is a better way.


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.