Hibernate - 约束名称

时间:2015-08-22 20:17:39

标签: java sql hibernate jpa orm

有人知道如何设置主键的约束名称(PK_ [name]),检查(CK_ [Name]),默认[DF_ [Name]],外键(Fk_ [Name]),Unique .. 例如,通过使用Annotations或xml配置,我想要Table ::::

CREATE TABLE ACCOUNT (
[ID] INT NOT NULL IDENTITY(1,1),
[USERNAME] VARCHAR(50) NOT NULL,
[PASSWORD] VARCHAR(50) NOT NULL,
[EMAIL] VARCHAR(100) NOT NULL,
[GENDER] INT NOT NULL ,
[ADDRESS] INT,
[AGE] INT CONSTRAINT DF_tblAccount_Age DEFAULT 0,
CONSTRAINT PK_tblAccount_ID PRIMARY KEY([ID]),
CONSTRAINT UQ_tblAccount_Address UNIQUE(ADDRESS),
CONSTRAINT FK_tblAccount_Gender FOREIGN KEY([GENDER]) REFERENCES GENDER([ID]),
CONSTRAINT FK_tblAccount_Address FOREIGN KEY([ADDRESS]) REFERENCES ADDRESS([ID]) ON DELETE SET NULL,
CONSTRAINT CK_tblAccount_Age CHECK ([AGE] > 0 AND [AGE] <100)
)

将其翻译成注释类或[name] .hbm.xml, Hibernate是否支持使用约束名称,或者我必须更改表 每次我创建一个新课程...... 我搜索了一下,我找到了0个关于约束名称的结果!!

注释类示例:::

@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
// -->>Add Primary Key constraint Name Here !! ???
private int id;

@Column(name="Name")
private String name;

@Column(name="Surname")
private String surname;

@Column(name="age")
// -->>Add Check constraint Name Here !! ???
private String age;

@OneToOne(cascade={CascadeType.ALL},fetch=FetchType.LAZY)
@OnDelete(action=OnDeleteAction.CASCADE)
@JoinColumn(name="Address" , referencedColumnName="id",nullable=false)
// -->>Add Foreign Key constraint Name Here !! ???
private Address address;

1 个答案:

答案 0 :(得分:5)

在JPA中,您可以使用@Table注释属性在类级别执行此操作,您可以在其中指定约束名称。对于映射到列id的主键id

@Table(uniqueConstraints = @UniqueConstraint(columnNames = "ID", name = "PERSONS_PK_CONSTRAINT"))