-->
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.  [ 2 posts ] 
Author Message
 Post subject: EJB 3 OneToMany with Composite Key
PostPosted: Wed Jul 04, 2007 9:37 am 
Newbie

Joined: Wed Jul 04, 2007 9:06 am
Posts: 12
Hi all,


Having a problem setting up my first EJB 3 OneToMany relationship with a composite key.

Here's the error:
14:09:01,812 WARN [JDBCExceptionReporter] SQL Error: 957, SQLState: 42000
14:09:01,812 ERROR [JDBCExceptionReporter] ORA-00957: duplicate column name

The 'One' Entity:
Code:


Code:
import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;



@Entity
@Table(name = "COMMS_MESSAGES")
public class CommsMessage implements Serializable
{

   private int messageId;

   /**
    * Any attachments.
    */
   private Collection<CommsAttachment> commsAttachments;


   public CommsMessage()
   {
   }


   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getMessageId()
   {
      return messageId;
   }
   
   public void setMessageId(int messageId)
      {
         this.messageId = messageId;
   }

   @OneToMany(mappedBy = "commsMessage", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch=FetchType.LAZY)
   public Collection<CommsMessageRecipient> getCommsMessageRecipients()
   {
      return commsMessageRecipients;
   }

   public void setCommsMessageRecipients(Collection<CommsMessageRecipient> commsMessageRecipients)
   {
      this.commsMessageRecipients = commsMessageRecipients;
   }

   

}




The 'Many' Entity:
Code:

   


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


@Entity
@Table(name = "COMMS_MESSAGE_RECIPIENTS")
@IdClass(CommsMessageRecipientPK.class)
public class CommsMessageRecipient implements Serializable
{
   private CommsMessage commsMessage;

   private int messageId;

   private int recipientUserId;


   public CommsMessageRecipient()
   {
   }

   @ManyToOne
   @JoinColumn(name = "messageid")
   public CommsMessage getCommsMessage()
   {
      return commsMessage;
   }

   public void setCommsMessage(CommsMessage commsMessage)
   {
      this.commsMessage = commsMessage;
   }

   @Id
   public int getMessageId()
   {
      return messageId;
   }

   public void setMessageId(int messageId)
   {
      this.messageId = messageId;
   }

   @Id
   public int getRecipientUserId()
   {
      return recipientUserId;
   }

   public void setRecipientUserId(int recipientUserId)
   {
      this.recipientUserId = recipientUserId;
   }
}




The Composite Key:
Code:
package com.msp.ejb.lms.comms;

import java.io.Serializable;

public class CommsMessageRecipientPK implements Serializable
{
   private int messageId;

   private int recipientUserId;


   public CommsMessageRecipientPK()
   {
   }

   public CommsMessageRecipientPK(int messageId, int recipientUserId)
   {
      this.messageId = messageId;
      this.recipientUserId = recipientUserId;
   }


   public int getMessageId()
   {
      return messageId;
   }

   public void setMessageId(int messageId)
   {
      this.messageId = messageId;
   }

   public int getRecipientUserId()
   {
      return recipientUserId;
   }

   public void setRecipientUserId(int recipientUserId)
   {
      this.recipientUserId = recipientUserId;
   }


   public boolean equals(Object o)
   {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      CommsMessageRecipientPK that = (CommsMessageRecipientPK) o;

      return messageId == that.messageId && recipientUserId == that.recipientUserId;

   }

   public int hashCode()
   {
      int result;
      result = messageId;
      result = 31 * result + recipientUserId;
      return result;
   }
}






As you can see, I'm trying to map the CommsMessage to multiple CommsRecipients using the 'messageid' value found in both tables.

Any help greatly appreciated.


Top
 Profile  
 
 Post subject: try hbm2java
PostPosted: Wed Jul 04, 2007 3:15 pm 
Newbie

Joined: Tue Nov 18, 2003 9:43 pm
Posts: 2
Location: Laurel, MD
I cannot stress enough how valuable the hibernate reverse engineering tools are when trying to understand mappings. Grab the seam distribution and run seam-gen against your database. It will automatically generate all the entities in a way optimized for Hibernate.

While you may not use the entities it creates, you can learn from the process. I bet the solution to your problem will become very clear when you do so. Besides, you may learn other things in the process.


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