I have a table called SecurityContacts and another table called Contacts. The association between them is ManyToOne. SecurityContacts has columns for both an AgentContact and an AuditContact, which are two different types of contacts. It also has a column for the security_id associated with both of those contacts. These two contacts join with the primary key of Contacts, which is contact_id.
Here is an example of the problem: Suppose an entry in SecurityContact exists as such: AgentContact = 5, AuditContact= NULL, security_id=1000. You try to set the AuditContact of that existing entry as 6. Hibernate creates a second entry as such: AgentContact=Null, AuditContact=6, security_id=1000. What should happen is that the existing entry should be modified to be: AgentContact = 5, AuditContact = 6, security_id=1000.
The interesting thing about this is that if both AgentContact and AuditContact are already set to any existing value in Contact, instead of NULL, the update works properly. This leads me to believe it might be an outer/inner join problem. Here is the code for my SecurityContacts table:
/**
* 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 = "" + securityContactId;
}
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="securityContact_id")
private Long securityContactId;
@Column(name="security_id")
private String 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 getSecurityContactId() {
return this.securityContactId;
}
public void setSecurityContactId(Long securityContactId) {
this.securityContactId = securityContactId;
}
public String getSecurityId() {
return this.securityId;
}
public void setSecurityId(String 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="parent", property="contactId")
public AgentContact getAgentContact() {
return this.agentContact;
}
public void setAgentContact(AgentContact agentContact) {
this.agentContact = agentContact;
}
@FXManyToOne(parent="parent", property="contactId")
public AgentContact getAuditContact() {
return this.auditContact;
}
public void setAuditContact(AgentContact auditContact) {
this.auditContact = auditContact;
}
}
Here is the code for my Contacts table:
/**
* 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="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="parent", 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;
}
}
Sorry for being so long-winded. Anyone have any ideas?
That all looks fairly normal to me. I suspect the problem might be in the code that does the loading/saving/updating. I tested a trimmed-down version of your entities, and adding an audit contact to a security contact that doesn’t already have one works fine. You can take a look at the code on github, or just clone the project and run it yourself:
You may have a different problem, though. Your
@OneToMany(mappedBy="agentContact")on the AgentContact class is only going to contain the SecurityContacts that have this AgentContact as theiragentContactproperty, not any that have it as theirauditContact. Also, all of that@Column(name="name with underscores")nonsense can be gotten rid of if you just use a NamingStrategy. The ImprovedNamingStrategy that ships with Hibernate already does the camel-case to underscore-separated conversion for you.