相关疑难解决方法(0)

JPA复合主键

我的JPA模型中有以下类(省略了getter,setter和不相关的字段):

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Currency {

    @Id
    private Integer ix;
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
}
Run Code Online (Sandbox Code Playgroud)

我需要定义一个类Price,使得当从所述类生成DDL时,相应的表的主键被由密钥ProductCurrency.我尝试过以下方法:

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@IdClass(PricePK.class)
public class Price {

    @Id @ManyToOne(optional = false)
    private Product product;

    @Id
    @ManyToOne(optional = false)
    private Currency currency;
}

@Embeddable
public class PricePK implements Serializable {

    Integer product;        
    Integer currency;
}
Run Code Online (Sandbox Code Playgroud)

但是这会为PRICE表生成以下内容:

create table …
Run Code Online (Sandbox Code Playgroud)

java sql-server maven-2 hibernate jpa

9
推荐指数
1
解决办法
3万
查看次数

标签 统计

hibernate ×1

java ×1

jpa ×1

maven-2 ×1

sql-server ×1