我一直在努力使用休眠创建一个复合键。我找到了这个链接: 如何制作复合主键(java持久性注释)
首先,我尝试了 Tim 接受的解决方案@NaturalId
。不适合我。
其次,我尝试了 Arthur Ronald FD Garcia 的解决方案。我仍然有一些问题。这是我的代码,与 Arthur Ronald FD Garcia 的解决方案非常相似。
邮箱类:
@Entity
public class Mailbox {
@Id
@GeneratedValue
private int mailboxId;
@Column( unique=true, nullable=false )
private String name;
@Column( unique=true, nullable=false )
private String employeeId;
private String status;
private Date createdOn;
@OneToMany(cascade=CascadeType.ALL, mappedBy="joinedMailboxMessageId.mailboxId")
private List<MailboxMessages> joinedMailboxMessageList = new ArrayList<MailboxMessages>();
public Mailbox(){}
public Mailbox(int id) {this.mailboxId=id;}
public void addSMail(SMail mail) {
// Implementation
}
//Getters and setters
}
邮件类
@Entity
public class SMail {
@Id
@GeneratedValue
private int messageId;
private Date originDate;
private String from;
@ElementCollection
private List<String> to=new ArrayList<String>();
@Lob
private String message;
private String subject;
@OneToMany(cascade=CascadeType.ALL, mappedBy="joinedMailboxMessageId.messageId")
private List<MailboxMessages> joinedMailboxMessageList = new ArrayList<MailboxMessages>();
public SMail() {}
public SMail(int messageId) {
this.messageId=messageId;
}
// addMailbox sets up bidirectional relationship
public void addMailbox(Mailbox mailbox) { // Implementation}
//Getters and setters
}
最后是我的 MailboxMessages 类
@Entity
public class MailboxMessages {
@ManyToOne
@JoinColumn(name="MAILBOX_ID", insertable=false, updatable=false)
private Mailbox mailboxId;
@ManyToOne
@JoinColumn(name="MESSAGE_ID", insertable=false, updatable=false)
private SMail messageId;
private boolean read;
private boolean deleted;
private boolean flagged;
private String priority;
private Date messageDate;
@EmbeddedId
// Implemented as static class - see bellow
private MailboxMessagesId joinedMailboxMessageId;
// INNER CLASS: required because this class contains composite id
@Embeddable
public static class MailboxMessagesId implements Serializable {
@ManyToOne
@JoinColumn(name="MAILBOX_ID")
private Mailbox mailboxId;
@ManyToOne
@JoinColumn(name="MESSAGE_ID")
private SMail messageId;
// required no arg constructor
public MailboxMessagesId() {}
public MailboxMessagesId(Mailbox mailbox, SMail mail) {
this.mailboxId = mailbox;
this.messageId = mail;
}
public MailboxMessagesId(int mailboxId, int messageId) {
this(new Mailbox(mailboxId), new SMail(messageId));
}
@Override
public boolean equals(Object instance) {
if (instance == null)
return false;
if (!(instance instanceof MailboxMessagesId))
return false;
final MailboxMessagesId other = (MailboxMessagesId) instance;
if (!(mailboxId.getMailboxId()==(other.getMailboxId().getMailboxId())))
return false;
if (!(messageId.getMessageId()==(other.getMessageId().getMessageId())))
return false;
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + (this.mailboxId != null ? this.mailboxId.hashCode() : 0);
hash = 47 * hash + (this.messageId != null ? this.messageId.hashCode() : 0);
return hash;
}
//Getters and setters
}
//Constructors and getters and setters
}
现在真正的问题是休眠只是为邮箱类创建表。所以我想问题可能出在其他两个班级。我尝试了亚瑟罗纳德的确切解决方案;它工作得很好。你能帮我找出我在代码中可能犯的错误吗?并且如果在 MailboxMessage 类中创建复合键的这种技术有任何替代方法
编辑1:
发现的问题之一是我使用“from”作为我的成员字段,它是 sql 的保留关键字。谁会想到呢?但问题仍然存在。现在我确实有一个用于 smail 类的表,但仍然没有为 MailboxMessages 类创建表。请帮帮我。