希望有人可以帮助我解决这个问题。
我正在尝试使用 JPA / Hibernate 在两个实体之间创建双向连接。
我遇到了这个问题,这似乎表明我的数据库列名需要与我的值对象属性名匹配——但这看起来很奇怪。希望有人可以通过提供一些清晰度来帮助我。
不幸的是,我所处的位置是一些明亮的火花用无意义的名称命名了列名。 我无法更改列名-相信我-如果可以的话-我会的!
所以这就是我的架构的样子: -
Customer
========
CS1 -- The ID column
CS2 -- Customer Description
ContactDetails
==============
CD1 -- The ID column
CD2 -- The FK to the Customer table
...
正如您所期望的那样,我的应用程序中有两个值对象 - 它们看起来像这样:-
客户.java
@Entity
@Table("Customer")
public class Customer {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CS1", nullable=false, columnDefinition="INTEGER")
private int id;
}
@OneToMany (cascade = CascadeType.ALL, mappedBy = "customerId", fetch = FetchType.EAGER)
private Set <ContactDetailsVO> contactDetails = new HashSet <CustomerDetailsVO> ();
}
ContactDetails.java
public class ContactDetails {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CD1", nullable=false, columnDefinition="INTEGER")
private int id;
@Column(name = "CD2")
long customerId;
@ManyToOne(optional = false)
@JoinColumn(name = "customerId", referencedColumnName = "id")
private CustomerVO customer;
public CustomerVO getCustomer() {
return customer;
}
public void setCustomer(CustomerVO customer) {
this.customer = customer;
}
}
不幸的是,这种引用对象属性名称的方法,因为映射关系似乎不起作用 - 我在启动时得到以下信息: -
Caused by: org.hibernate.HibernateException: Missing column: customerId in TestDB.dbo.ContactDetails
at org.hibernate.mapping.Table.validateColumns(Table.java:276)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:131
因此,如果我随后更改映射以使用实际的表列名,我会得到以下信息:-
客户.java
@Entity
@Table("Customer")
public class Customer {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CS1", nullable=false, columnDefinition="INTEGER")
private int id;
}
@OneToMany (cascade = CascadeType.ALL, mappedBy = "CD2", fetch = FetchType.EAGER)
private Set <ContactDetailsVO> contactDetails = new HashSet <CustomerDetailsVO> ();
}
ContactDetails.java
public class ContactDetails {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CD1", nullable=false, columnDefinition="INTEGER")
private int id;
@Column(name = "CD2")
long customerId;
@ManyToOne(optional = false)
@JoinColumn(name = "CD2", referencedColumnName = "CS1")
private CustomerVO customer;
public CustomerVO getCustomer() {
return customer;
}
public void setCustomer(CustomerVO customer) {
this.customer = customer;
}
}
然后我得到以下信息:-
Caused by: org.hibernate.MappingException: Could not determine type for:
com.myCompany.model.vo.CustomerVO, at table: ContactDetails, for columns:
[org.hibernate.mapping.Column(customer)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
正如我所说,解决这个问题的明显方法是命名我的对象属性,与表列名称相同 - 但考虑到命名表列的愚蠢,我宁愿将我的应用程序与它隔离开来。
有人可以帮助阐明这种奇怪的行为吗?
提前致谢。