簡體   English   中英

從JPA批注生成DDL

[英]generate DDL from JPA annotations

我的JPA模型中有以下類(省略了getters,setters和無關字段):

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

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

    @Id
    private Integer ix;
}

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

@MappedSuperclass
public abstract class AutoIdPersistentEntity extends PersistentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
} 

我使用hibernate3 Maven插件的hbm2ddl目標從這些類生成DDL。 它為與Price類相對應的表生成以下內容

create table PRICE (
    currency_id int null,
    product_id int null,
    primary key (currency_id, product_id)
);

請注意, currency_idproduct_id都是可為空的,當我嘗試將DDL加載到SQL Server時會導致以下錯誤

無法在表“ PRICE”中的可為空的列上定義PRIMARY KEY約束

我不明白為什么這些都是可為空的,因為在域模型中它們被標注為@ManyToOne(optional = false)

使用org.hibernate.dialect.SQLServerDialect SQL方言生成DDL。

這有點令人困惑,但是我認為某些JPA實現未將可選屬性(@ ManyToOne,@ Basic等)用於模式生成。

我認為您需要使用@JoinColumn批注並將nullable設置為false: http : //download.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html

(或者在非聯接情況下為@Column: http : //download.oracle.com/javaee/6/api/javax/persistence/Column.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM