希望有人能幫助我解決這個問題。JPA Hibernate父/子映射混淆
我試圖創建使用JPA/Hibernate的兩個實體之間的雙向連接。
我有這個問題,這似乎指出,我的數據庫列名稱需要匹配我的價值對象屬性名稱 - 但這似乎很奇怪。希望有人能幫助我,提供一些清晰。
不幸的是,我處於一個明亮的星星命名列名無意義名稱的位置。 我不能改變列名 - 相信我 - 如果可以的話 - 我會的!
因此,這裏是我的架構是什麼樣子: -
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>();
}
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
所以,如果我再改變映射使實際使用中的表的列名,而不是,我得到如下: -
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 = "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)
正如我說,要解決這個明顯的方式就是將其命名我的對象的屬性,一樣的表列名稱 - 但考慮到命名錶列的愚蠢,我寧願將它隔離我的應用程序。
有人可以幫助揭開這種奇怪的行爲嗎?
在此先感謝。