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> PK mapping to a <composite-key>
PostPosted: Fri Dec 21, 2007 10:53 am 
Newbie

Joined: Fri Dec 21, 2007 10:20 am
Posts: 5
Hello, I've scoured the net for an answer but I haven't been able to find one, I guess I must a newb but here it goes. I will try to be as clear as possible.

I have a class A which has a composite key identifier:

Its impl would look like:

Code:
public class A {
  public final static class Id implements Serializable {
    public Long aId1;
    public Long aId2;
  }

  private Id id;

  public Id getId() { return id; }
  public void setId(Id id) { this.id = id; }

  <... getters/setters/other code here ...>
}


and its mapping a trivial:

Code:
<class name="A" table="A_TBL">
  <composite-id class="A$Id" name="id">
    <key-property access="field" column="id1" name="aId1" type="long"/>
    <key-property access="field" column="id2" name="aId2" type="long"/>
  </composite-id>
</class>


Simple right? Well I want to create a one-to-one mapping to this class without having to change the implementation code or the mapping file (they are generated) from a class B:

B is even more simple, although maybe I'm missing something:

Code:
public class B {
 
  private A.Id id;
  private A a;

  public A.Id getId() { return id; }
  public void setId(A.Id id) { this.id = id; }

  public A getA() { return a; }
  public void setA(A a) { this.a = a; }

  <... getters/setters/other code here ...>
}


And here is the mapping, and where I think the error lies:

Code:
<class name="B" table="B_TBL">
  <id name="id" type="A$Id">
    <column name="id1"/>
    <column name="id2"/>
    <generator class="foreign">
      <param name="property">a</param>
    </generator>
  </id>
  <one-to-one name="a" class="A" constrained="true"/>
</class>


Ok for me this seems simple enough but now I can successfully add the mapping files to the session factory (I use addURL()) but when I attempt to build the session factory, I get this exception:

org.hibernate.MappingException: identifier mapping has wrong number of columns: dummy.B type: dummy.A$Id
at org.hibernate.mapping.RootClass.validate(RootClass.java:194)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:1287)
...

Now I've looked a little into the code and well there is something I don't understand. In my B mapping class I want the primary key to be 2 columns wide like in A (duh same key ;)). But well the type of the $Id class is SerializableType and well the getColumnSpan() of it always returns 1 (inhereted from NullableType). Is this a possible bug?

If anyone can please help I would be most appreciative. Thanks again :)


Top
 Profile  
 
 Post subject: Fix
PostPosted: Wed Jan 23, 2008 1:41 pm 
Newbie

Joined: Thu Nov 16, 2006 3:48 pm
Posts: 3
Did you fix this issue. I have the same kind of issue with my code. If you do please let me know the solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 6:04 am 
Newbie

Joined: Fri Dec 21, 2007 10:20 am
Posts: 5
I was never able to fix the issue.. I just had to resign myself by creating a surrogate id and a one-to-one mapping as follows:
Code:
<class name="B" table="B_TBL">
  <id name="id" column="id" type="integer" unsaved-value="null">
    <generator class="native"/>
  </id>
  <many-to-one name="a" class="A" unique="true" not-null="true">
    <column name="id1" not-null="true"/>
    <column name="id2" not-null="true"/>
  </many-to-one>
</class>


The java class remains unchanged. Hope this helps :)


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.