复合键的Hibernate解决方案



我一直在使用hibernate创建一个组合键。我找到了这个链接:如何创建复合主键(java持久化注释)

首先,我用@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
}

简讯邮件类

@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只为邮箱类创建表。所以我想问题可能出在另外两门课上。我尝试了亚瑟·罗纳德的精确解决方案;而且效果很好。你能帮我找出我在代码中可能犯的错误吗?如果在MailboxMessage类

中创建复合键有任何替代方法

EDIT1:

发现的问题之一是我使用'from'作为我的成员字段,这是为sql保留关键字。谁会想到这一点呢?但问题依然存在。现在我确实有一个小类的表,但仍然没有为MailboxMessages类创建表。

我认为这是解决方案,例如字段名称只是示例:

@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;

我自己找到了解决方案:问题是使用'from'作为SMail类的数据成员,'read'作为MailboxMessages类的数据成员,因为它们是mysql的保留关键字。现在我所有的表都创建好了,更不用说复合键了。(通过数据库确认)

相关内容

最新更新