Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: inverse-one-to-one generates a set
PostPosted: Fri Mar 11, 2011 7:27 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
For some reason, <inverse-one-to-one> generates a Set instead of a simple pointer.
I'm a bit surprised it's generating a Set instead of emitting an error message explaining why it must be a <many-to-one>.

Details (nonkey columns elided except for one example in each table):

The table being referenced:
Code:
CREATE TABLE "HF_DI_BVKRIT" (
  "BKID" NUMBER(27,0) NOT NULL ENABLE,
  "VERSION" NUMBER(18,0) DEFAULT 0 NOT NULL ENABLE,
  "BKNAME" VARCHAR2(40) NOT NULL ENABLE
) ;
ALTER TABLE "HF_DI_BVKRIT" ADD CONSTRAINT "HF_DI_BVKRIT_PK"
  PRIMARY KEY ("BKID") ENABLE;
The table with the foreign key:
Code:
CREATE TABLE "HF_DI_BVKOPF" (
  "BVID" NUMBER(10,0) NOT NULL ENABLE,
  "VERSION" NUMBER(18,0) DEFAULT 0 NOT NULL ENABLE,
  "BVBKID" NUMBER(27,0) NOT NULL ENABLE,
  "BVGPRS" NUMBER(8,2)
) ;
ALTER TABLE "HF_DI_BVKOPF" ADD CONSTRAINT "HF_DI_BVKOPF_PK"
  PRIMARY KEY ("BVID") ENABLE;
ALTER TABLE "HF_DI_BVKOPF" ADD CONSTRAINT "HF_DI_BVKOPF_UK"
  UNIQUE ("BVBKID") ENABLE;
ALTER TABLE "HF_DI_BVKOPF" ADD CONSTRAINT "HF_DI_BVKOPF_FK_BVKRIT"
  FOREIGN KEY ("BVBKID") REFERENCES "HF_DI_BVKRIT" ("BKID") ENABLE;
Relevant section from my hibernate.reveng.xml:
Code:
  <table name="HF_DI_BVKOPF">
    <column name="VERSION" type="long"></column>
    <foreign-key constraint-name="HF_DI_BVKOPF_FK_BVKRIT">
      <one-to-one />
      <inverse-one-to-one />
    </foreign-key>
  </table>
This generates the following Java classes:
Code:
public class HfDiBvkrit implements java.io.Serializable {
  private BigInteger bkid;
  private long version;
  private String bkname;
  private Set <HfDiBvkopf> hfDiBvkopfs = new HashSet <HfDiBvkopf> (0);
  ...
}
public class HfDiBvkopf implements java.io.Serializable {
  private BigInteger bvid;
  private long version;
  private HfDiBvkrit hfDiBvkrit;
  private BigDecimal bvgprs;
  ...
}

The problem here is Set <HfDiBvkopf> hfDiBvkopfs. Having a member here is okay (it's the backlink for HfDiBvkrit hfDiBvkrit in HfDiBvkopf), but it should be of type HfDiBvkopf, not Set <HfDiBvkopf>.

Note that the unique key on BVBKID makes it impossible for multiple BVKOPF records to reference the same BVKRIT record. I had expected that unique key would make Reveng recognize this as a (weak) one-to-one association.

Oh, and yes, I'm 100% sure that <inverse-one-to-one> generates a Set.
With <inverse-one-to-one exclude="true"/>, I don't get the Set (but no non-Set backlink either).

Is there already a JIRA entry that the reference docs don't explain the usage of <one-to-one> and <inverse-one-to-one>?
Such as the following information bits (please correct any factual errors im making! - this is my current understanding of what reveng does, if I'm wrong, I have no chance of this ever getting to work):
- Explaining that the foreign key needs to reference a full primary or unique key on the foreign table, else it would be a one-to-many relationship (which would have to be declared on the other table anyway)
- Explaining that the foreign key needs to be unique (PK or just a unique index), else we have a many-to-one relationship and people should use <many-to-one> and <set> instead of <one-to-one> and <inverse-one-to-one>.


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Fri Mar 11, 2011 9:04 am 
Senior
Senior

Joined: Tue Aug 04, 2009 7:45 am
Posts: 124
Joachim Durchholz wrote:
The problem here is Set <HfDiBvkopf> hfDiBvkopfs. Having a member here is okay (it's the backlink for HfDiBvkrit hfDiBvkrit in HfDiBvkopf), but it should be of type HfDiBvkopf, not Set <HfDiBvkopf>.

Why do you think so?
Quote:
Note that the unique key on BVBKID makes it impossible for multiple BVKOPF records to reference the same BVKRIT record. I had expected that unique key would make Reveng recognize this as a (weak) one-to-one association.

Is not a prove. It only prove that it should be OneToSomething reference.
As HF_DI_BVKRIT table doesn't have any reference to HF_DI_BVKOPF Hibernate could make any choice(OneTo <Many or One> ). It selects OneToMany and that is why you have Set


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Fri Mar 11, 2011 10:36 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Dmitry Geraskov wrote:
Joachim Durchholz wrote:
The problem here is Set <HfDiBvkopf> hfDiBvkopfs. Having a member here is okay (it's the backlink for HfDiBvkrit hfDiBvkrit in HfDiBvkopf), but it should be of type HfDiBvkopf, not Set <HfDiBvkopf>.
Why do you think so?
For the reasons below ;-D
Dmitry Geraskov wrote:
Quote:
Note that the unique key on BVBKID makes it impossible for multiple BVKOPF records to reference the same BVKRIT record. I had expected that unique key would make Reveng recognize this as a (weak) one-to-one association.
Is not a prove. It only prove that it should be OneToSomething reference.
Agreed. (Might be SomethingToOne actually, I'm not 100% sure. It doesn't change anything substantial anyway.)
Dmitry Geraskov wrote:
As HF_DI_BVKRIT table doesn't have any reference to HF_DI_BVKOPF Hibernate could make any choice(OneTo <Many or One> ). It selects OneToMany and that is why you have Set
Right, but there's also the foreign key on BVKOPF.BVBKID to BVKRIT.BKID. I think it "imports" the PK-induced uniqueness of BKID to the other side of the association.
The result should be the combination of *-to-one and a one-to-*, resulting in one-to-one.

Is this correct, or did I miss something?


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Fri Mar 11, 2011 10:53 am 
Senior
Senior

Joined: Tue Aug 04, 2009 7:45 am
Posts: 124
Joachim,
I thought again about the problem and now I agree with you.
We need to support this trick in DefaultReverseEngineeringStrategy#isOneToOne()
Could you please create a feature request in jboss jira?
You can use your own strategy as a workaround.


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Fri Mar 11, 2011 12:35 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Done.
I hope I described everything correctly.

I was lucky and found out that, after all, I don't need any backlink generation yet, so the workaround isn't needed. Might change in the future, so thanks for the hint!


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Mon Nov 18, 2013 1:16 pm 
Newbie

Joined: Mon Nov 18, 2013 1:03 pm
Posts: 1
Hi hibernate!

Im facing the same problem as Joachim Durchholz. Does anybody had changed the DefaultReverseEngineeringStrategy#isOneToOne ?

Joachim Durchholz finally didnt used the backlink, so i suppose he did:

Code:
<table name="HF_DI_BVKOPF">
    <column name="VERSION" type="long"></column>
    <foreign-key constraint-name="HF_DI_BVKOPF_FK_BVKRIT">
      <one-to-one />
      <inverse-one-to-one  exclude="true"/>
    </foreign-key>
  </table>


Anyway would be great to have the complete functionality of relationship mappings


Top
 Profile  
 
 Post subject: Re: inverse-one-to-one generates a set
PostPosted: Tue Nov 19, 2013 6:15 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Given that the JIRA issue has been lying dormant for over two years now, and no work on Hibernate Tools has been commited in that time as well, I consider Hibernate Tools dead and buried.
I switched to doing reverse engineering by hand. That proved to be less hairpulling, less time and less effort than trying to make Hibernate Tools reveng work as I needed it.
I heartily recommend doing likewise. No reveng can add the usertype conversions and relationships as you really need them, this information simply isn't available in the database, so you'll be manually reviewing every field anyway. Reveng is useful for an initial view of how to map the various situations, but after that, go and study the annotations and apply them over to any new tables that you add. It's far easier than trying to shoehorn Hibernate Tools into generating what you want.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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.