-->
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: key-many-to-one annotation issue
PostPosted: Tue Dec 05, 2006 4:10 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
Hibernate version:
3.2.0GA

I've found a funny issue while using hibernate3 with ejb3.

I have a wierd table structure:
LegacyTable - old table in our DB with a composite key
LegacyExtendedTable - new table that is a child of the Legacy Table with an extra field
NewTable - a new, and simple table (one Id field that is generated by hibernate)
ManyToManyTable - between LegacyExtendedTable and NewTable with an extra field, so I have to write it out in hibernate as a new class.

I was able to get everything working without issue until working on the many-to-many table. Its using a key of two many-to-ones to two tables. One being a simple Id, the other a composite key.

Initially I used the following (I had to genericise the code, so my apologizes for minor syntax issues)
Code:
@Table(name = "manyToManyTable")
public class ManyToManyTable
{
   private int extraField;
   private ManyToManyTablePk pk;

  @Column(name="whatever") public int getExtraField(){return extraField;}
  @Id ManyToManyTablePk pk;
}

@Embeddable
public class ManyToManyTablePk
{
   private LegacyExtendedTable legacy;
   private NewTable new;

    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "PK_NO"), @JoinColumn(name = "PK_NO_2"),
        @JoinColumn(name = "PK_INT")})
   public LegacyExtendedTable getLegacy() { return legacy; }

    @ManyToOne
    @JoinColumn(name = "NEW_ID")
   public NewTable getNew() { return new; }
}


On deploy, Hibernate complained that the NewTable FK was bad because the NewTable code didn't have all four FK constraints. I figured this was because hibernate mapped the annotation like a many-to-one instead of a key-many-to-one. I replaced the code with the following

Code:
@Table(name = "manyToManyTable")
@IdClass(ManyToManyTablePk.class)
public class ManyToManyTable
{
   private int extraField;
   private LegacyExtendedTable legacy;
   private NewTable new;

  @Column(name="whatever") public int getExtraField(){return extraField;}

    @Id
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "PK_NO"), @JoinColumn(name = "PK_NO_2"),
        @JoinColumn(name = "PK_INT")})
   public LegacyExtendedTable getLegacy() { return legacy; }

    @Id
    @ManyToOne
    @JoinColumn(name = "NEW_ID")
   public NewTable getNew() { return new; }
}

@Embeddable
public class ManyToManyTablePk
{
   private LegacyExtendedTable legacy;
   private NewTable new;

   public LegacyExtendedTable getLegacy() { return legacy; }

   public NewTable getNew() { return new; }
}


And the code worked, fine.

It looks like key-many-to-one only works when you put an @Id tag before the @ManyToOne. If the @ManyToOne is embedded in a PK class, and you don't use the @IdClass method in your object, it doesn't recognize it as a key-many-to-one.

Is anyone else noticing this? I don't want to mark it up as a bug unless someone else sees a similar situation.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Weird, I have testcase for both in org.hibernate.test.annotations.idmanytoone

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 2:06 pm 
Beginner
Beginner

Joined: Sun Nov 16, 2003 3:04 pm
Posts: 24
Do both examples use composite pks for the many-to-ones?

I have to wait to get home to d/l the source and test cases, but anon browsing I glanced and it looks like storecustomer uses @IdClass with a non annotatied PK class, but Card uses @Id that isn't composite (although I'm not with the html browser, and just glanced at it). I'll get back to you later tonight or tomorrow night when I've had a chance to check out the test case.

Thanks for the response!


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.