-->
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.  [ 9 posts ] 
Author Message
 Post subject: "optional" association
PostPosted: Thu May 26, 2005 10:56 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
Hi,
I have a class named Address and a class named Country.
The address can have exactly one or none country.
In the database there is a table address and a table country. Both have their own id's.
The table address has a column named country_id.
If the address has a country there is the id from country in it and else there is a 0.
When I load my address I just want to load the associated country, if there is no 0 in the column of country_id.

How can I map this with Hibernate?

Another problem is, that when I load all countries and view them in a selectbox and when I want to add a country to a address an save the address, I get a TransientObjectException, because the country is ot saved. But I never want to save my country, I just want to get it out of the database and will never modify it.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
So, what's wrong with using null to represent, um, nulls? Then you would be able to do really good stuff like .... referential integrity.

If this is legacy data and you really *can't* fix the broken database try using not-found="ignore".

But MUCH better to fix the broken data.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:20 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
When there is a 0 in the database I got a exception:
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 0

Because he looks after the primary key 0 in the country tagle, but can't find it. and I want him to load the country, when the id is not 0.

Is there a possibility to tell Hibernate, that he just retrieve the country, when the id in the address table is not 0?

Would it be better to load every association alone intead of loading my address and automatically load all associated objects?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
not-found="ignore", like I just told you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:30 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
I did the following in the address.hbm.xml:

<many-to-one
name="country"
class="Country"
column="country_id"
unique="true"
cascade="none"
insert="false"
update="false"
not-found="ignore"/>

But the exception was thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Show me the stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 11:54 am 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
its not from the address, but from the company. the compny can have a crmStatus.

Hibernate: select this.company_id as company_id3_, this.company_name as company_2_3_, this.phone as phone3_, this.phone2 as phone23_, this.fax as fax3_, this.url as url3_, this.employees as employees3_, this.profile as profile3_, this.remark as remark3_, this.default_primary_address as default10_3_, this.entered_at as entered_at3_, this.entered_by as entered_by3_, this.last_modified_at as last_mo13_3_, this.last_modified_by as last_mo14_3_, this.crm_status_id as crm_sta15_3_, this.industry_id as industr16_3_, this.user_id as user_id3_, crmstatus1_.crm_status_id as crm_stat1_0_, crmstatus1_.crm_status_short_name as crm_stat2_0_, crmstatus1_.crm_status_pretty_name as crm_stat3_0_, industry2_.industry_id as industry1_1_, industry2_.industry_short_name as industry2_1_, industry2_.industry_pretty_name as industry3_1_, user3_.user_id as user_id2_, user3_.username as username2_, user3_.password as password2_ from companies this left outer join crm_statuses crmstatus1_ on this.crm_status_id=crmstatus1_.crm_status_id left outer join industries industry2_ on this.industry_id=industry2_.industry_id left outer join users user3_ on this.user_id=user3_.user_id where this.company_id=?

Hibernate: select crmstatus0_.crm_status_id as crm_stat1_0_, crmstatus0_.crm_status_short_name as crm_stat2_0_, crmstatus0_.crm_status_pretty_name as crm_stat3_0_ from crm_statuses crmstatus0_ where crmstatus0_.crm_status_id=?

ERROR [http-8080-Processor25] (CompanyDAO.java:112) - Hibernate Exception

net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 0, of class: de.woco.moslokal.businessobject.CrmStatus
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1964)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:208)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2219)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:319)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:309)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:118)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3660)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238)
at net.sf.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:385)
at de.woco.moslokal.persistencelayer.CompanyDAO.getCompanyByOid(CompanyDAO.java:96)
at de.woco.moslokal.service.BusinessDelegate.getCompanyByOid(BusinessDelegate.java:140)
at de.woco.moslokal.action.CompanyAction.getCompanyByOid(CompanyAction.java:433)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
[WARN] SessionImpl - afterTransactionCompletion() was never called


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use Hibernate3.

not-found is not even available in HB2! Your XML validation should have failed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 12:52 pm 
Newbie

Joined: Thu May 26, 2005 10:40 am
Posts: 11
ok, thank you a lot.


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