我一直在使用hibernate创建复合键。我找到了这个链接: how to make a composite primary key (java persistence annotation)
首先,我用@NaturalId
尝试了Tim接受的解决方案。没有为我工作。
其次,我尝试了Arthur Ronald F D Garcia的解决方案。我仍然遇到一些问题。这是我的代码,与Arthur Ronald F D 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
}
SMail课程
@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
}
现在真正的问题是hibernate只为邮箱类创建表。所以我想问题可能在其他2个类中。我尝试了亚瑟·罗纳德的确切解决方案;它工作得很好。你能帮我辨别一下我在代码中可能犯的错误吗? 此外,如果在MailboxMessage类中创建复合键的技术有替代方法
EDIT1:
发现的问题之一是我使用'from'作为我的成员字段,它是sql的保留关键字。谁会想到这个?但问题仍然存在。现在我有一个用于smail类的表,但仍然没有为MailboxMessages类创建表。请帮帮我。
答案 0 :(得分:0)
我认为这是解决方案,例如字段名称只是示例:
@Id
@Column(name = "EMP_NO_FK" , unique=true , nullable = false ,length=10)
private Long empno;
@Id
@Column(name = "GROUP_NO_FK" , unique=true , nullable = false ,length=5)
private Long groupNo;
@Id
@Column(name = "DEPT_NO_FK" , unique=true , nullable = false ,length=10)
private Long deptno;
@Id
@Column(name = "YEAR_NAME_M_FK" , unique=true , nullable = false ,length=4)
private Integer year;
答案 1 :(得分:0)
自己找到解决方案:问题在于使用'from'作为SMail类中的数据成员,并将'read'作为MailboxMessages类中的数据成员,因为它们是使用mysql的保留关键字。现在我的所有表都是创建的,更不用说复合键了。 (通过数据库确认)