-->
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.  [ 9 posts ] 
Author Message
 Post subject: referencing unique part of a composite key fails
PostPosted: Wed Jan 20, 2010 9:58 am 
Newbie

Joined: Thu Jun 25, 2009 5:42 am
Posts: 7
Hi,

I have a table with a composite key consisting of two columns. One of these two columns has a UNIQUE constraint. Another table references this unique column with a single foreign key.

This seems impossible with Hibernate. It looks something like this:

Code:
@Entity
@IdClass(A.AId.class)
public class A {

   @Id
   @Column(unique = true)
   private long akey;
   
   @Id
   private long anotherkey;
   
   // ID class, getter and setter follow

}
//--------------------------------------------------------------
@Entity
public class B {

   @Id
   private long id;
   
   @ManyToOne
   @JoinColumn(name = "akey", referencedColumnName = "akey")
   private A a;

   // getter and setter follow

}

The deploy fails with an error message like
Code:
referencedColumnNames(akey) of package.B.a referencing package.A not mapped to a single property


Huh? What is that supposed to mean? And how can I fix it? I found a few similar problems but no solutions.

Thanks in advance
mK


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Wed Jan 20, 2010 11:36 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Please attach also the implementation code of A.AId.class
Furthermore I do not clear understand if A and B are pure class names or if you are using A also a package name (not do it).
Assuming that A is not part of the package name, I deduct that you defined Aid as inner class of A.
From my experience hibernate has problems with persistent inner classes.
Try to define Aid as normal class (Aid.java) in the same package as where class A and class B are.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Thu Jan 21, 2010 6:06 am 
Newbie

Joined: Thu Jun 25, 2009 5:42 am
Posts: 7
Thanks for your reply. OK, I guess I was a bit vague regarding the IdClass. A.AId is as you guessed an inner class. But since you mention problems with Hibernate and inner classes I moved it in another public class:
Code:
@Entity
@IdClass(AId.class)
public class A {

   @Id
   @Column(unique = true)
   private long akey;
   
   @Id
   private long anotherkey;
   
   // getter and setter follow

}

public class AId implements Serializable {
   public long akey;
   public long anotherkey;
}

Class B remains untouched and the error from my first post still exists. One slight correction though: The example from my first post was untested; the error message is
Code:
Caused by: org.hibernate.AnnotationException: referencedColumnNames(akey) of pkg.B.a referencing pkg.A not mapped to a single property

Of course the word package itself is not a valid package name, so I now chose pkg.

When I remove the @ManyToOne constraint anything seems to work so the primary key of class A should not be an issue (but well, you never know).

If anybody has the time to try this example, my table definitions are as follows:
Code:
CREATE TABLE a ( akey BIGINT UNIQUE, anotherkey BIGINT, PRIMARY KEY(akey, anotherkey) );
CREATE TABLE b ( id BIGINT PRIMARY KEY, akey BIGINT, FOREIGN KEY(akey) REFERENCES a(akey) );


Greetings
mK


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Fri Jan 22, 2010 5:35 am 
Newbie

Joined: Thu Jun 25, 2009 5:42 am
Posts: 7
This post originally stated that the problem is solved. Turned out to be wrong... sorry for double posting.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Mon Jan 25, 2010 10:06 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:

   @Id
   @Column(unique = true)
   private long akey;
   
   @Id
   private long anotherkey;



Actually, this definition above makes no sense, spoken in UML.
If akey is declared unique, then it has absolutely no sense to span the @Id annotation also over another key.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Mon Jan 25, 2010 12:45 pm 
Newbie

Joined: Thu Jun 25, 2009 5:42 am
Posts: 7
While this is true this shouldn't be Hibernate's concern. I didn't invent the schema and would prefer not to change it because the database is what you could call mission critical. So I have no idea whether tampering with this might break something else.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Wed Jan 27, 2010 4:38 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
It seems that on
http://opensource.atlassian.com/project ... e/HHH-4014
they found a solution for your problem.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Fri Jan 29, 2010 11:11 am 
Newbie

Joined: Thu Jun 25, 2009 5:42 am
Posts: 7
To be honest I don't see much in common with the problem at hand. Maybe I miss something fundamental.

Also I'm using JPA so I don't have a .hbm.xml but rather a persistence.xml and annotated classes. Maybe there are differences as well. I once read that the JPA spec says you can't reference a column if it is not a primary key. But after digging a while through the spec I can't find a statement like this.


Top
 Profile  
 
 Post subject: Re: referencing unique part of a composite key fails
PostPosted: Fri Jan 29, 2010 11:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Also I'm using JPA ... I once read that the JPA spec says you can't reference a column if it is not a primary key. But after digging a while through the spec I can't find a statement like this.


Also I'm using JPA and do in successfull way reference not primary key columns.
I do it only in a slightly different way as you:

Code:
@Entity
public class CompanyPeer {
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "OID")
    private long oid;
...
    @javax.persistence.OneToMany(mappedBy = "parentCompany")
    @javax.persistence.MapKey(name="year")
     protected Map<Integer,CommitmentDataPeer> aggCommitmentData = new java.util.HashMap();

}

@Entity
@org.hibernate.annotations.Table( appliesTo = "CommitmentData", indexes = {@org.hibernate.annotations.Index( name = "CommitmentDataPARENT9923" , columnNames = {"parentCompany"})
public class CommitmentDataPeer
{
   @Id @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "OID")
    private long oid;
 
    @Column
    protected int year;

    @javax.persistence.ManyToOne(fetch = FetchType.LAZY)
    protected CompanyPeer parentCompany = null;
}


Maybe it might be useful to you ...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.