如何为复合主键创建JPA类

时间:2017-03-09 11:11:23

标签: java hibernate jpa spring-boot composite-primary-key

我有复合主键表

  

site_Uid - >来自站点表
  address_Uid - >来自地址   表

以下JPA类抛出异常

@Entity @IdClass(Site.class)
@Table(name = "site_address")
public class SiteAddress implements Serializable {

    private static final long serialVersionUID = 7237095501517057347L;
    @Id
    @Column(name = "site_uid")
    private int siteUid;
    @Column(name = "address_uid")
    @Id
    private int addressUid;
}

网站类:

@Entity
@Table(name = "site")
public class Site implements Serializable {

    private static final long serialVersionUID = 2059474818667179203L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long siteUid;
    @Column(name = "party_role_uid")
    private int partyRoleUid;
    @Column(name = "site_type_cd")
    private int siteTypeCd;
    @Column(name = "site_name")
    private String siteName;

}

错误:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to find properties (siteTypeCd, siteName, partyRoleUid) in entity annotated with @IdClass:com.enkindle.model.SiteAddress

1 个答案:

答案 0 :(得分:3)

创建一个包含两个字段的SiteAddressId类:int siteUidint addressUid。然后在SiteAddress中执行:@IdClass(SiteAddressId.class)

有关详细信息,请参阅此处:http://www.objectdb.com/java/jpa/entity/id#Composite_Primary_Key_