简体   繁体   English

hibernate4中的默认值

[英]default value in hibernate4

I have Object class which is mapped to a oracle database table. 我有映射到oracle数据库表的Object类。 in this class, one column has a default value in database. 在此类中,数据库中的一列具有默认值。 I used this: @Column(name = "FK_STATUS" ,nullable = false, columnDefinition = "int default 1") the column type in database is Number. 我用了这个:@Column(name =“ FK_STATUS”,nullable = false,columnDefinition =“ int default 1”)数据库中的列类型是Number。 but I get "can not insert null in FK_status" error. 但出现“无法在FK_status中插入null”错误。 please help. 请帮忙。

As i didn't see any syntax errors there in your line,Not much aware of H-3 as i'm using H-3. 由于我没有在您的行中看到任何语法错误,因此我在使用H-3时不太了解H-3。

This could be a fix,Not exactly solution for your problem. 这可能是一个解决方法,不能完全解决您的问题。

Use a default value to your variable . variable使用默认值。

private Integer fkStatus= 1;

The columnDefinition will work columnDefinition将起作用

columnDefinition = "int default 1" (should be "default 1")

but you have to recreate DB tables first. 但是您必须首先重新创建数据库表。 It will set your column to set null values as int 1 它将设置您的列以将空值设置为int 1

Well, you should recreate DB via 好吧,您应该通过以下方式重新创建数据库

persistence.xml persistence.xml

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>

or do it manually 或手动执行

update {TABLE} 
set FK_STATUS = 1 
where status is null;

alter table {TABLE} modify (FK_STATUS default 1 NOT NULL);

@NGS, The constraints such as unique, nullable, default etc.. are the keywords which the JPA/Hibernate will use during the table creation. @NGS,唯一性,可空性,默认性等约束是JPA / Hibernate在表创建期间将使用的关键字。 It doesn't have any significance after table creation. 创建表后,它没有任何意义。 For Eg, If you create a table first with a column as non-unique, and if you are planning to control the uniqueness of the column by providing annotation, will not work. 例如,如果您首先创建一个表,该表的一列不唯一,并且您计划通过提供注释来控制该列的唯一性,那么它将无法正常工作。

All those kind of metadata are provided for the table creation. 为创建表提供了所有这些类型的元数据。

I would like to do small correction in your Hibernate-Mapping Column code , please see below 我想在您的Hibernate-Mapping Column代码中进行小的更正,请参见下文

@Column(name = "FK_STATUS" ,nullable = false, columnDefinition = "int(10) default 1")

Used this and let me know , If this is not working with your code 使用了这个,让我知道,如果这不适用于您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM