-->
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.  [ 4 posts ] 
Author Message
 Post subject: ManyToMany as 2 ManyToOne relationships
PostPosted: Sat Nov 19, 2005 4:58 pm 
Newbie

Joined: Sat Nov 19, 2005 3:57 pm
Posts: 2
I'm trying to get a Many to Many relationship with additional data to work using annotations. I've read that this is done using a couple of OneToMany (or the inverse, 2 ManyToOne) relationships with an intermediate Entity.The approach actually works, but I haven't been able to do it without having to set an Id on the association table. What I want to do is use the combination of the 2 foreign keys from the associated tables as the parent key for the entity, and avoid using an unnecesary Id in the relationship entity.

An example would be:
Users {user_id, ...}
Messages {message_id, ...}

and the relationship I want to create is such that many users can receive multiple messages, and set an additional status field in that relationship:
ReceivedMessages {user_id, message_id, status}

My code for the association entity is:
Code:
package ejb;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "message_recipients")
public class MessageRecipient implements Serializable {

    private Long id;
    private Message message
    private User recipient;
    private Integer status;

    public MessageRecipient() {
    }

    @Id(generate = GeneratorType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "message_id")
    public Message getMessage {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    @ManyToOne
    @JoinColumn(name = "recipient_id")
    public User getRecipient() {
        return recipient;
    }

    public void setRecipient(User recipient) {
        this.recipient = recipient;
    }

    @Column(name = "status")
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }


}


If I try the same code without setting the Id field and @Id annotation, it fails.Does anyone know how to avoid using the id and/or how to set the user_id - message_id pair to be unique via annotations?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 20, 2005 4:11 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Look in to @IdClass and @EmbeddedId. Although, unless dealing w/ a legacy system, the use of composite primary keys is discouraged.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 4:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use

@IdClass


and
@Column(name="column1")
public String entityPk;

plus

@ManyToOne
@JoinColumn(name="column1" insert=false update=false)
public Entity myEntity;

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:18 pm 
Newbie

Joined: Sat Nov 19, 2005 3:57 pm
Posts: 2
Thanks for your replies.

I'm trying to use the using a class for the Parent Key approach. However, I can't get it right. It works if I just declare it without making the proper references to the Foreign Keys needed in the PK class.

I'm getting the following error when trying to do the mapping:

Code:
org.hibernate.MappingException: Repeated column in mapping for entity: ejb.MessageRecipient column: msg_id (should be mapped with insert="false" update="false")


which I'm pretty sure has to do with what emmanuel indicates. However, the insert and update properties do not seem to be supported (perhaps it should be insertable and updatable, but the weird thing is that the error message says the same).

Also, I'm confused about the "String entityPk" on the suggested code... I'm trying to use a composite key, why should I declare that PK reference and where? Also the same goes for the @ManyToOne annotation... should that be on the PK class? or should I be referencing the class that uses the PK class from the PK class? I've tried both with absolutely no luck.

I've tried getting it to work in many different combinations and still the closest I seem to get is getting the entity embedded on the class when I don't try to have it make use of the Foreign key...

Any suggestions?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.