-->
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.  [ 4 posts ] 
Author Message
 Post subject: forcing a one-to-one with reverse engineering
PostPosted: Wed Sep 28, 2011 10:57 am 
Newbie

Joined: Thu Oct 23, 2003 8:19 am
Posts: 14
Location: Tampa, FL USA
I'm doing an hbm2java with Hibernate Tools 3.2.4.GA

I have tables like this:

Code:
create table DOG (
   dog_id  numeric(10)  primary key,
   name    varchar(15)   not null
);

create table BONE (
   bone_id    numeric(10)  primary key,
   dog_id     numeric(10)  not null,
   desc       varchar(15)  not null,
       CONSTRAINT dog_id_fk FOREIGN KEY (dog_id) REFERENCES DOG(dog_id)
);


The result in class Dog is:

Code:
private Set<Bone> bones = new HashSet<Bone>(0);


and in class B:

Code:
private Dog dog;



which is an expected default. However, I would like to FORCE that all dogs only have one bone, as that is the business policy by convention, and would prefer to not have to write code which constantly grabs the first item from a Set. I would like the Dog class to just have:

Code:
private Bone bone;


In my hibernate.reveng.xml, I have tried this:

Code:
<table name="BONE">
   <foreign-key constraint-name="dog_id">
      <one-to-one property="dog" exclude="false"/>
      <inverse-one-to-one property="bone" exclude="false"/>
   </foreign-key>
</table>


but in Dog I still get the Set<Bone> bones and no Bone bone

-Steve
Tampa, FL


Top
 Profile  
 
 Post subject: Re: forcing a one-to-one with reverse engineering
PostPosted: Wed Sep 28, 2011 11:56 am 
Newbie

Joined: Thu Oct 23, 2003 8:19 am
Posts: 14
Location: Tampa, FL USA
just realized that I should probably put the proper name for the constraint, but it didn't help anything.

Code:
<table name="BONE">
   <foreign-key constraint-name="dog_id_fk">
      <one-to-one property="dog" exclude="false"/>
      <inverse-one-to-one property="bone" exclude="false"/>
   </foreign-key>
</table>


Top
 Profile  
 
 Post subject: Re: forcing a one-to-one with reverse engineering
PostPosted: Wed Sep 28, 2011 1:24 pm 
Newbie

Joined: Thu Oct 23, 2003 8:19 am
Posts: 14
Location: Tampa, FL USA
It would appear that I have tripped over a bug, as explained here: https://forum.hibernate.org/viewtopic.php?f=6&t=1009989 and entered into Jira here: https://hibernate.onjira.com/browse/HBX-1179

When exclude the inverse-one-to-one, as described, my Set<Bone> bones in Dog does go away:

Code:
<table name="BONE">
   <foreign-key constraint-name="dog_id_fk">
      <one-to-one property="dog" exclude="false"/>
      <inverse-one-to-one exclude="true"/>
   </foreign-key>
</table>


That is only a bit of a workaround though, for something which would appear to be, as of yet, broken in the tool.

Is it even possible to define a one-to-one relationship from Dog to Bone, based solely on a "backlink" from Bone to Dog?


Top
 Profile  
 
 Post subject: Re: forcing a one-to-one with reverse engineering
PostPosted: Wed Sep 28, 2011 2:33 pm 
Newbie

Joined: Thu Oct 23, 2003 8:19 am
Posts: 14
Location: Tampa, FL USA
something that could generate:

Code:
@OneToOne(mappedBy=dog)
private Bone bone;


would be cool, but I just don't see a way to get hbm2java to do that

I can cure the @ManyToOne that gets generated on dog in the Bone class, by excluding the one-to-one. ... equally confusing in the same way that excuding inverse-one-to-one gets rid of the @OneToMany Set in Dog.

Code:
<table name="BONE">
   <column name="dog_id" property="dog" type="Dog"/>
   <foreign-key constraint-name="dog_id_fk">
      <one-to-one exclude="true"/>
      <inverse-one-to-one exclude="true"/>
   </foreign-key>
</table>


Notice also that I can try to manually add the dog property back in, but it becomes kinda useless without the "@OneToOne(cascade = CascadeType.ALL)" and "@JoinColumn(name="dog_id")" annotations.


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