-->
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: one-to-one to itself
PostPosted: Sun Sep 25, 2005 4:56 am 
Newbie

Joined: Sat Sep 24, 2005 12:24 pm
Posts: 7
Hi all,

I want to declare a One-To-One association to the same class.
Like
Person -> substiude -> Person

my mapping is:

<class name="Person">

<id name="id">
<generator class="native"/>
</id>
<property name="substituteId" unique="true"/>
<property name="name"/>

<one-to-one name="substitude" class="Person"
cascade="all" foreign-key="substituteId"/>
</class>

But no foreign key is genrated to the substitudeId.
What is my mistake?
Can someone help?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 10:47 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
I think that, in order to achieve your goal, you should use a mapping like:

Code:
<class name="Person">
  <id name="id">
    <generator class="native"/>
  </id>
  <property name="name"/>
  <many-to-one name="substituteId" class="Person" cascade="all" unique="true"/>
</class>


Erik


Top
 Profile  
 
 Post subject: Hi
PostPosted: Sun Sep 25, 2005 12:21 pm 
Newbie

Joined: Sat Sep 24, 2005 12:24 pm
Posts: 7
This helps a little bit. Now the foreign key is created but
when I call
Code:
p2.setSubstituteId(1);

I get an exception

Code:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of org.hibernate.test.propertyref.Person.id
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119)
   at org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103)
   at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2944)
   at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2705)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
   at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
   at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
   at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
   at org.hibernate.test.propertyref.PropertyRefTest.testOneToOnePropertyRef(PropertyRefTest.java:40)
   at org.hibernate.test.TestCase.runTest(TestCase.java:129)
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 12:52 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
I think it should be
p2.substituteId(<Person object with ID=1>) instead of p2.substituteId(1).
Since you are mapping Objects here, the Arguments need to reflect the same.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 1:01 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
rajasaur wrote:
I think it should be
p2.substituteId(<Person object with ID=1>) instead of p2.substituteId(1).
Since you are mapping Objects here, the Arguments need to reflect the same.

That's correct (except for substituteId which should read setSubstituteId): this is the way the mapping I proposed is meant to be used.

Erik


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.