Hibernate分配不正确的数据库ID

时间:2011-07-18 15:04:55

标签: java hibernate

情况就是这样:我有一个映射表,我们称之为SecurityContact,有三列。

  1. 第一列(我们称之为security_id)包含SecurityContact的主键。 Hibernate成功自动生成此列的ID号。
  2. 第二列(我们称之为agent_id)包含第二个表的主键值(我们称之为AgentContact),其主键列名为contact_id。您可能已经猜到,此agent_id列与引用的列contact_id是SecurityContact和AgentContact之间的连接列之一。
  3. SecurityContact的第三列(我们称之为audit_id)也包含AgentContact主键的值。这也是SecurityContact和AgentContact之间的连接列,再次引用了AgentContact中的列contact_id。 SecurityContact中的条目与AgentContact中的条目之间的关系是多对一的。
  4. 基本上,这个SecurityContact表将agent_id和audit_id连接到security_id。 agent_id和audit_id可以相同或不同,具体取决于AgentContact中的两个条目是相同还是不同。

    现在问题就在于:即使两个条目不同,Hibernate似乎也为它们都设置了相同的id,这个id是SecurityContact的auditContact的contact_id。这两个条目应该不同,agent_id包含代理的contact_id,audit_id包含审计的contact_id(来自同一个表,AgentContact)。任何人都知道为什么?

    我为这么长时间的啰嗦而道歉。以下是SecurityContact的代码:

     /**
     * The persistent class for the SecurityContact database table.
     * 
     */
    @Entity
    @FXClass(kind=FXClassKind.REMOTE)
    public class SecurityContact implements Serializable {
        private static final long serialVersionUID = 1L;
        @Transient private String uid;
        @FXIgnore
        public String getUid() {
            if (uid == null) {
                uid = "" + securityId;
            }
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="security_id")
        private Long securityId;
    
        @Column(name="create_date")
        private String createDate;
    
        @Column(name="create_user")
        private String createUser;
    
        @Column(name="modify_date")
        private String modifyDate;
    
        @Column(name="modify_user")
        private String modifyUser;
    
        //bi-directional many-to-one association to AgentContact
        @ManyToOne
        @JoinColumn(name="agent_id", referencedColumnName="contact_id")
        private AgentContact agentContact;
    
        //bi-directional many-to-one association to AuditContact
        @ManyToOne
        @JoinColumn(name="audit_id", referencedColumnName="contact_id")
        private AgentContact auditContact;
    
        public SecurityContact() {
        }
        @FXKeyColumn
        public Long getSecurityId() {
            return this.securityId;
        }
    
        public void setSecurityId(Long securityId) {
            this.securityId = securityId;
        }
    
        public String getCreateDate() {
            return this.createDate;
        }
    
        public void setCreateDate(String createDate) {
            this.createDate = createDate;
        }
    
        public String getCreateUser() {
            return this.createUser;
        }
    
        public void setCreateUser(String createUser) {
            this.createUser = createUser;
        }
    
        public String getModifyDate() {
            return this.modifyDate;
        }
    
        public void setModifyDate(String modifyDate) {
            this.modifyDate = modifyDate;
        }
    
        public String getModifyUser() {
            return this.modifyUser;
        }
    
        public void setModifyUser(String modifyUser) {
            this.modifyUser = modifyUser;
        }
        @FXManyToOne(parent="com.counterpartcontacts.entity.AgentContact", property="contactId")
        public AgentContact getAgentContact() {
            return this.agentContact;
        }
    
        public void setAgentContact(AgentContact agentContact) {
            this.agentContact = agentContact;
        }
        @FXManyToOne(parent="com.counterpartcontacts.entity.AgentContact", property="contactId")
        public AgentContact getAuditContact() {
            return this.auditContact;
        }
    
        public void setAuditContact(AgentContact auditContact) {
            this.auditContact = auditContact;
        }
    
    }
    

    以下是AgentContact表的代码:

    /**
     * The persistent class for the AgentContact database table.
     * 
     */
    @Entity
    @FXClass(kind=FXClassKind.REMOTE)
    public class AgentContact implements Serializable {
        private static final long serialVersionUID = 1L;
        @Transient private String uid;
        @FXIgnore
        public String getUid() {
            if (uid == null) {
                uid = "" + contactId;
            }
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="contact_id")
        private Long contactId;
    
        @ManyToOne
        @JoinColumn(name="bank_id")
        private Bank bank;
    
    //  @Column(name="bank_id")
        //private String bank2;
    
        @Column(name="create_date")
        private String createDate;
    
        @Column(name="create_user")
        private String createUser;
    
        private String email;
    
        private String fax;
    
        @Column(name="modify_date")
        private String modifyDate;
    
        @Column(name="modify_user")
        private String modifyUser;
    
        private String name;
    
        private String phone;
    
        //bi-directional many-to-one association to SecurityContact
        @OneToMany(mappedBy="agentContact")
        private Set<SecurityContact> securityContacts;
    
    
        public AgentContact() {
        }
        @FXKeyColumn
        public Long getContactId() {
            return this.contactId;
        }
    
        public void setContactId(Long contactId) {
            this.contactId = contactId;
        }
        @FXManyToOne(parent="com.counterpartcontacts.entity.Bank", property="bankId")
        public Bank getBank() {
            return this.bank;
        }
    
        public void setBank(Bank bank) {
            this.bank = bank;
        }
    
        public String getCreateDate() {
            return this.createDate;
        }
    
        public void setCreateDate(String createDate) {
            this.createDate = createDate;
        }
    
        public String getCreateUser() {
            return this.createUser;
        }
    
        public void setCreateUser(String createUser) {
            this.createUser = createUser;
        }
    
        public String getEmail() {
            return this.email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getFax() {
            return this.fax;
        }
    
        public void setFax(String fax) {
            this.fax = fax;
        }
    
        public String getModifyDate() {
            return this.modifyDate;
        }
    
        public void setModifyDate(String modifyDate) {
            this.modifyDate = modifyDate;
        }
    
        public String getModifyUser() {
            return this.modifyUser;
        }
    
        public void setModifyUser(String modifyUser) {
            this.modifyUser = modifyUser;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPhone() {
            return this.phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
        @FXOneToMany(fillArguments="contactId")
        public Set<SecurityContact> getSecurityContacts() {
            return this.securityContacts;
        }
    
        public void setSecurityContacts(Set<SecurityContact> securityContacts) {
            this.securityContacts = securityContacts;
        }
    
    
    }
    

0 个答案:

没有答案