-->
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: Transient instance problem
PostPosted: Fri May 27, 2005 3:37 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
Hi,
I have a table in my database named country and table named address.
When I want to create a new address I get all the countries from the database and show them in a select box. After getting the values from the jsp site I want to create my address, so I add the country to the address and want to save it. One address has exactly one country.
Then I get the exception:
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing.

My problem is that I never want to save the country, because it is in the database and I just want to show it in the view and want to add it to a address.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:20 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Show your mapping pls.

When you mean to get the "country", do u create new instance of Country and assign it to the adress?

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:35 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
In Java it looks like this:

Struts Action class:
// I get the country oid from the select box from the view
Country country = businessDelegate.getCountryByOid(new Long(Long.parseLong(countryOid)));
address.setCountry(country);
businessDelegate.saveAddress(address);

DAO classes:
public Country getCountryByOid(Long countryOid){
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
country = (Country) session.get(Country.class, countryOid);
tx.commit();
HibernateUtil.closeSession();
...
}

public void saveAddress(Address address){
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
session.save(address);
tx.commit();
HibernateUtil.closeSession();
...
}

Address.java:
private Country country = new Country();
// with setter and getter

Mapping of address:
<many-to-one
name="country"
class="de.woco.moslokal.businessobject.Country"
column="country_id"
unique="true"
cascade="none"
lazy="true"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 4:39 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
Steffi wrote:
In Java it looks like this:

Struts Action class:
// I get the country oid from the select box from the view
Country country = businessDelegate.getCountryByOid(new Long(Long.parseLong(countryOid)));
address.setCountry(country);
businessDelegate.saveAddress(address);

DAO classes:
public Country getCountryByOid(Long countryOid){
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
country = (Country) session.get(Country.class, countryOid);
tx.commit();
HibernateUtil.closeSession();
...
}

public void saveAddress(Address address){
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
session.save(address);
tx.commit();
HibernateUtil.closeSession();
...
}

Address.java:
private Country country = new Country();
// with setter and getter

Mapping of address:
<many-to-one
name="country"
class="de.woco.moslokal.businessobject.Country"
column="country_id"
unique="true"
cascade="none"
lazy="true"/>



1. In adress java, do not create new Country as;
private Country country = new Country();
let it be private Country country;

2. In DAO part , you dont need a transaction for getting objects

Try now.

_________________
-developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 5:00 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
juhu, it works :)
thank you very much.


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.