JPA Hibernate父/子映射混淆

时间:2013-03-28 13:04:02

标签: hibernate jpa mapping

希望有人可以帮助我解决这个问题。

我正在尝试使用JPA / Hibernate在两个实体之间创建双向连接。

我遇到了这个问题,这似乎指出了这样一个事实:我的数据库列名需要匹配我的Value Object属性名 - 但这看起来很奇怪。希望有人可以通过提供一些清晰度来帮助我。

不幸的是,我处在这样一个位置:一些明亮的火花已经将列名称命名为毫无意义的名称。 我无法更改列名称 - 相信我 - 如果可以的话 - 我愿意!

所以这就是我的架构: -

Customer
========
CS1 -- The ID column
CS2 -- Customer Description

ContactDetails
==============
CD1 -- The ID column
CD2 -- The FK to the Customer table
...

正如您所期望的,我在应用程序中有两个Value对象 - 它们看起来像这样: -

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;
}
}

不幸的是,这种引用Object属性名称的方法对于映射关系似乎不起作用 - 我在启动时得到以下内容: -

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)

正如我所说,解决这个问题的显而易见的方法是命名我的对象属性,与表列名相同 - 但考虑到命名表列的愚蠢,我宁愿将我的应用程序与它隔离开来。

有人可以帮助阐明这种奇怪的行为吗?

提前致谢。

1 个答案:

答案 0 :(得分:-1)

好的 - 我认为这是Hibernate中的一个真正的问题。基本上如果你混合属性和方法注释装饰方法,Hibernate会给你带来伪造,毫无意义的错误。希望这可以为我节省6个小时的时间。