JPA Hibernate父/子映射混淆
希望有人能帮我做这件事 我正在尝试使用JPA/Hibernate在两个实体之间创建双向连接 我遇到了这个问题,这似乎表明了一个事实,即我的数据库列名需要与我的值对象属性名匹配——但这似乎很奇怪。希望有人能帮助我,提供一些清晰 不幸的是,我正处在这样一个位置:一些明亮的火花用毫无意义的名字来命名列名我无法更改列名-相信我-如果可以-我会的强> 我的模式是这样的:-JPA Hibernate父/子映射混淆,hibernate,jpa,mapping,Hibernate,Jpa,Mapping,希望有人能帮我做这件事 我正在尝试使用JPA/Hibernate在两个实体之间创建双向连接 我遇到了这个问题,这似乎表明了一个事实,即我的数据库列名需要与我的值对象属性名匹配——但这似乎很奇怪。希望有人能帮助我,提供一些清晰 不幸的是,我正处在这样一个位置:一些明亮的火花用毫无意义的名字来命名列名我无法更改列名-相信我-如果可以-我会的 我的模式是这样的:- Customer ======== CS1 -- The ID column CS2 -- Customer Description C
Customer
========
CS1 -- The ID column
CS2 -- Customer Description
ContactDetails
==============
CD1 -- The ID column
CD2 -- The FK to the Customer table
...
正如您所期望的,在我的应用程序中,我有两个值对象-它们如下所示:-
Customer.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> ();
}
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;
}
}
@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> ();
}
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.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
因此,如果我随后更改映射以使用实际的表列名,则会得到以下结果:-
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)
Customer.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> ();
}
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;
}
}
@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> ();
}
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)
正如我所说,解决这个问题的明显方法是命名我的对象属性,与表列名相同——但是考虑到命名表列的愚蠢性,我宁愿将我的应用程序与之隔离
有人能帮我解释一下这种奇怪的行为吗
提前谢谢。好的-我认为这是Hibernate中的一个真正问题。基本上,如果混合使用属性和方法注释装饰方法,Hibernate会给您带来虚假的、无意义的错误。希望这能帮别人省下我刚才浪费的6个小时