如何为复合主键创建 JPA class

How to create JPA class for composite primary key

我有 table 复合主键

site_Uid --> from Site table
address_Uid --> from Address table

下面的 JPA class 抛出异常

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

站点 Class:

@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

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

查看此处了解更多信息:http://www.objectdb.com/java/jpa/entity/id#Composite_Primary_Key_