users@glassfish.java.net

Composite primary key and unidirectional manytomany relation

From: <glassfish_at_javadesktop.org>
Date: Thu, 03 May 2007 06:33:56 PDT

Hi
I have following scenario
Tables:
User(userid, username)
Session(sessionid,sessionname)
Event(Eventid, eventname)

UserSessionEvent(userid,sessionid,eventid)

In the above relation userid is a FK to User and so on.

{userid,sessionid,eventid}

form a composite primary key.

Now I am trying to implement it as follows:

/*ENTITY CORRESPONDING TO USERSESSIONEVENT RELATION*/
@Entity
@Table(name = "usersessionevent")
@IdClass(UserSessionPK.class)
public class UserSessionEvent implements java.io.Serializable{
        @Id @ManyToOne @JoinColumn(name = "sessionid")
        private Session session;
        
        @Id @ManyToOne @JoinColumn(name = "usrid")
        private User user;
        
        @Id @ManyToOne @JoinColumn(name = "eventid")
        private Event event;
.....


/*PRIMARY KEY CLASS*/
public class UserSessionPK implements java.io.Serializable{
        private User user;
        private Session session;
        private Event event;
        public Event getEvent() {
                return event;
        }
        public void setEvent(Event event) {
                this.event = event;
        }
        public Session getSession() {
                return session;
        }
        public void setSession(Session session) {
                this.session = session;
        }
        public User getUser() {
                return user;
        }
        public void setUser(User user) {
                this.user = user;
        }
        public UserSessionPK(User user, Session session, Event event) {
                super();
                this.user = user;
                this.session = session;
                this.event = event;
        }
        public UserSessionPK() {
                super();
        }
}


However, I get an exception as follows:
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [entity.UserSessionPK] and those of the entity bean class [class entity.UserSessionEvent] must correspond and their types must be the same. Also, ensure that you have specified id elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.
[Message sent by forum member 'ketancmaheshwari' (ketancmaheshwari)]

http://forums.java.net/jive/thread.jspa?messageID=215341