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.