Hibernate assigning incorrect database ID's
Here's the situation: I have a mapping table, let's call it SecurityContact, with three columns.
- The first column(let's call it security_id) contains the primary key for SecurityContact. Hibernate successfully auto-generates an ID number for this column.
- The second column(let's call it agent_id) contains the value of the primary key of a second table (let's call it AgentContact), whose primary key column is called contact_id. This agent_id column, with the referenced column contact_id, as you may have guessed, is one of the join columns between SecurityContact and AgentContact.
- The third column of SecurityContact(let's call it audit_id) also contains the value of a primary key of AgentContact. This is also a join column between SecurityContact and AgentContact, with again the referenced column contact_id within AgentContact. The relationship between entries in SecurityContact and entries in AgentContact is many to one.
So basically, this SecurityContact table connects agent_id and audit_id to security_id. agent_id and audit_id can be the same or different, depending on whether the two entries in AgentContact are the same or different.
Now here's the problem: Even when the two entries are different, Hibernate seems to put the same id for both of them, this id being the contact_id of the auditContact of the SecurityContact. The two entries should be different, with agent_id containing the contact_id of the agent, and audit_id containing the contact_id of the audit (from the same table, AgentContact). Anyone have any idea why?
My apologies in advance for being so long winded. Here is the code for 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;
}
}
Here is the code for the AgentContact table:
/**
* The persistent class for the AgentContact database table.
*
*/
@Entity
@FXClass(kind=FXClassKind.REMOTE)
public class AgentContact implements S开发者_高级运维erializable {
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;
}
}
精彩评论