-->
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: Mapping collection of elements with composite key
PostPosted: Tue Dec 06, 2011 4:25 am 
Newbie

Joined: Tue Dec 06, 2011 4:15 am
Posts: 2
Hibernate version: 3.6.7.Final
Hibernate annotations version: 3.2.0.Final

I am trying to map classes over a pre-existing database that I cannot change. There is a parent table X and a child table Y. Y has a foreign key to X and another column that form a composite key.

Classes look like this:

Code:
@Entity
@Table(name = "X")
public class X {
    @Id
    @Column(name = "ID")
    private Integer id;
   
    // Getters & setters
}

@Embeddable
public class YKey {
    @ManyToOne
    @JoinColumn(name = "X_ID")
    private X x;
    @Column(name = "ORD")
    private Integer order;
   
    // Getters and setters
}

@Entity
@Table(name = "Y")
public class Y {
    @Id
    private YKey yKey;

    // Other fields
    // Getters and setters
}


How do I map the collection of Y's in class X using annotations?

It should be a List that is ordered by YKey.order

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Mapping collection of elements with composite key
PostPosted: Tue Dec 06, 2011 2:52 pm 
Newbie

Joined: Mon Jun 21, 2010 9:03 am
Posts: 7
Hi!

We had the same problem and we solved it this way.

Code:
@Entity
@Table(name = "X")
public class X {
    @Id
    @Column(name = "ID")
    private Integer id;
   
    // Getters & setters
}

@Embeddable
public class YKey {
   
    @Column(name="XID")
    private Long xId;
    @Column(name = "ORD")
    private Integer order;
   
    // Getters and setters
}

@Entity
@Table(name = "Y")
public class Y {
    @Id
    private YKey yKey;

    @MapsId("xId")
    @ManyToOne
    @JoinColumn(name = "X_ID")
    private X x;

    // Other fields
    // Getters and setters
}


Best regards.


Top
 Profile  
 
 Post subject: Re: Mapping collection of elements with composite key
PostPosted: Tue Dec 06, 2011 8:55 pm 
Newbie

Joined: Tue Dec 06, 2011 4:15 am
Posts: 2
Hi,

Great, thanks very much for replying, didn't know about the MapsId annotation. Very useful indeed!


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.