hibernate:表格没有id时

有些表没有定义id或主键,只有一个unique约束,你又无权修表。

但Hibernate说,一定要id,怎么办?

这时,可以把unique约束相关的field定义为id,一个类里,可以有多个@Id 。

这样可以省去写@EmbeddedId的麻烦。


下面是hibernate文档摘录:

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/

---------------------2.2.3.2.2. Multiple @Id properties

Another, arguably more natural, approach is to place @Id on multiple properties of my entity. This approach is only supported by Hibernate but does not require an extra embeddable component.

@Entity
class Customer implements Serializable {
  @Id @OneToOne
  @JoinColumns({
    @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
    @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
  })
  User user;
  
  @Id String customerNumber;

  boolean preferredCustomer;
}
 In this case Customer being it's own identifier representation, it must implement Serializable.
------------------------------------------------------------------
守护
守护

猜你喜欢

转载自bg090721.iteye.com/blog/1550685