-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-to-one relationship using the same class
PostPosted: Wed Aug 26, 2009 7:34 pm 
Newbie

Joined: Wed May 24, 2006 9:45 pm
Posts: 10
Hi,

I have a class named FinancialTransaction with some attributes, like date, amount, etc.
But there are 3 that are important here: Type, siblingTransaction and target.

The "type" attribute indicates if this is a Debit, Credit or Transfer FinancialTransaction.
For Debit and Credit transactions, target and siblingTransaction attributes are irrelevant.

But if I have a Transfer transaction I want to create 2 transactions:
The first one with 'type' == 'T', negative value and 'target' == false (this is the source)
The second one must have 'type' == 'T', positive value, and 'target' == true

Both transaction should know each other using the 'siblingTransaction' attribute. So I want to get something like this:
Code:
First.getSibling() == Second (target == true)
Second.getSibling() == First (target == false, this is the source)

How should I proceed to map a one-to-one relationship with the same class/table?
I have tried something like these:
Code:
<one-to-one name="siblingTransaction" />
<one-to-one name="siblingTransaction" class="package.Transaction"/>


Right now I'm getting a NullPointerException, no matter what relationship declaration I use:
Code:
java.lang.NullPointerException
   at br.com.rulz.domain.Transaction$$BulkBeanByCGLIB$$893d231e.setPropertyValues(<generated>)
   at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)
   at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)
   at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
   at org.hibernate.loader.Loader.doQuery(Loader.java:717)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2145)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
   at org.hibernate.loader.Loader.list(Loader.java:2024)
   at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
   at br.com.rulz.dao.impl.TransactionDAOImpl.findTransactionsByParams(TransactionDAOImpl.java:165)
   at br.com.rulz.business.impl.TransactionBusinessImpl.findTransactionsByDateRange(TransactionBusinessImpl.java:152)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:165)
   at $Proxy8.findTransactionsByDateRange(Unknown Source)
   at br.com.rulz.web.struts.action.transaction.TransactionAction.filterTransactions(TransactionAction.java:186)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
   at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
   at br.com.rulz.web.struts.action.BaseRulzAction.execute(BaseRulzAction.java:34)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at br.com.rulz.web.filter.LoginFilter.doFilter(LoginFilter.java:37)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Unknown Source)


This mapping is possible to achieve with hibernate? Am I missing some concept in my object model? Can anyone tell what is the error cause?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: One-to-one relationship using the same class
PostPosted: Thu Aug 27, 2009 8:58 am 
Newbie

Joined: Wed May 24, 2006 9:45 pm
Posts: 10
Guys,

The exception have nothing to do with the mapping. The cause of the NullPointerException was that I had a boolean property named target, wich was mapped as a tinyint column in my database.

The column was new and had several null values. So, hibernate wasn't able to call setTarget(boolean) once null isn't true nor false.

Update all columns to '0' or '1' could solve the problem, I guess. But in my case, for other reason, the column mapping was changed to varchar and the property became a String.


Top
 Profile  
 
 Post subject: Re: One-to-one relationship using the same class
PostPosted: Thu Aug 27, 2009 9:22 am 
Newbie

Joined: Wed May 24, 2006 9:45 pm
Posts: 10
Guys,

I'm still having problems to map a one-to-one relationship with my financial transactions.
I was able to map the relation in the hbm file and my application runs ok.

But the fields of the relationship aren't saved. Can anyone help me to find out what is going on?

In my code I do the following:
Code:
financialTransaction.setSiblingTransaction(theSiblingTransaction) // A -> B
financialTransaction.getSiblingTransaction().setSiblingTransaction(financialTransaction) // B -> A


My hbm have a one-to-one declared as follow:
Code:
<one-to-one property="siblingTransaction"/>


When the rows are created, the got the correct values, but the field "sibling_transaction_id" is null. How should I proceed to make hibernate persist the "theSiblingTransaction.id" when creating the "A" row? i.e, A.sibing_transaction_id == B.id

If it is complicated to understand my business domain, think about a class named "Person" and that each person have another person as a partner (husband/wife/spouse...)

I want to create person A and persist person B.id in field A.partner_id and persist A.id in field B.partner_id.
How sould I proceed?

Thanks.


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