首页 文章

在JPA中设置列的默认值

提问于
浏览
205

是否可以为JPA中的列设置默认值,以及是否使用注释完成了哪些操作?

18 回答

  • 26

    看到我在试图解决同样的问题时偶然发现Google的这个问题,我只是想提一下我做过的解决方案以防有人发现它有用 .

    从我的观点来看,这个问题真的只有一个解决方案 - @PrePersist . 如果你在@PrePersist中进行,你必须检查是否已经设置了值 .

  • 13

    实际上它在JPA中是可能的,虽然使用 @Column 注释的 columnDefinition 属性的一点点黑客,例如:

    @Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'")
    
  • 260

    您可以执行以下操作:

    @Column(name="price")
    private double price = 0.0;
    

    那里!您刚刚使用零作为默认值 .

    请注意,如果您只是从此应用程序访问数据库,这将为您服务 . 如果其他应用程序也使用数据库,那么您应该使用Cameron's columnDefinition注释属性或其他方式从数据库进行此检查 .

  • 2

    另一种方法是使用javax.persistence.PrePersist

    @PrePersist
    void preInsert() {
       if (this.createdTime == null)
           this.createdTime = new Date();
    }
    
  • 6

    在2017年,JPA仍然只有 @Column(columnDefinition='...') ,您可以在其中放置列的文字SQL定义 . 这是非常不灵活的,并迫使您也声明其他方面,如类型,短路JPA实现的观点 .

    Hibernate虽然,有这个:

    @Column(length = 4096, nullable = false)
    @org.hibernate.annotations.ColumnDefault("")
    private String description;
    

    标识要通过DDL应用于关联列的DEFAULT值 .

    两个注意事项:

    1)唐_1287271已经看到了相当多的规范过程 . 该规范基本上是特定领域的大型参与者愿意承诺支持未来十年左右的基线 . 对于安全性而言,对于消息传递来说,ORM是没有区别的(虽然JPA涵盖了很多) . 我作为开发人员的经验是,在复杂的应用程序中,无论如何您迟早都需要非标准的API . 并且 @ColumnDefault 是一个例子,它超过了使用非标准解决方案的负面影响 .

    2)它不一样 . 批量SQL更新怎么样?没有设置列的语句怎么样? DEFAULT 有's role and that'不能通过初始化Java类成员来替代它 .

  • 93

    JPA不支持这一点,如果有的话会很有用 . 使用columnDefinition是特定于数据库的,在许多情况下是不可接受的 . 当您检索具有空值的记录时(在重新运行旧的DBUnit测试时通常会发生这种情况),在类中设置默认值是不够的 . 我这样做是:

    public class MyObject
    {
        int attrib = 0;
    
        /** Default is 0 */
        @Column ( nullable = true )
        public int getAttrib()
    
        /** Falls to default = 0 when null */
        public void setAttrib ( Integer attrib ) {
           this.attrib = attrib == null ? 0 : attrib;
        }
    }
    

    Java自动拳击对此有很大帮助 .

  • 1
    @Column(columnDefinition="tinyint(1) default 1")
    

    我刚刚测试了这个问题 . It works just fine. 感谢您的提示 .


    关于评论:

    @Column(name="price") 
    private double price = 0.0;
    

    这个 doesn't 设置数据库中的默认列值(当然) .

  • 0

    我使用 columnDefinition 并且效果非常好

    @Column(columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    
    private Date createdDate;
    
  • 5

    您不能使用列注释执行此操作 . 我认为唯一的方法是在创建对象时设置默认值 . 也许默认构造函数是正确的地方 .

  • 7

    你可以用java反映api:

    @PrePersist
        void preInsert() {
           PrePersistUtil.pre(this);
        }
    

    这很常见:

    public class PrePersistUtil {
    
            private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    
            public static void pre(Object object){
                try {
                    Field[] fields = object.getClass().getDeclaredFields();
                    for(Field field : fields){
                        field.setAccessible(true);
                        if (field.getType().getName().equals("java.lang.Long")
                                && field.get(object) == null){
                            field.set(object,0L);
                        }else if    (field.getType().getName().equals("java.lang.String")
                                && field.get(object) == null){
                            field.set(object,"");
                        }else if (field.getType().getName().equals("java.util.Date")
                                && field.get(object) == null){
                            field.set(object,sdf.parse("1900-01-01"));
                        }else if (field.getType().getName().equals("java.lang.Double")
                                && field.get(object) == null){
                            field.set(object,0.0d);
                        }else if (field.getType().getName().equals("java.lang.Integer")
                                && field.get(object) == null){
                            field.set(object,0);
                        }else if (field.getType().getName().equals("java.lang.Float")
                                && field.get(object) == null){
                            field.set(object,0.0f);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    
  • 197

    在我的例子中,我修改了hibernate-core源代码,以便引入一个新的注释 @DefaultValue

    commit 34199cba96b6b1dc42d0d19c066bd4d119b553d5
    Author: Lenik <xjl at 99jsj.com>
    Date:   Wed Dec 21 13:28:33 2011 +0800
    
        Add default-value ddl support with annotation @DefaultValue.
    
    diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/DefaultValue.java b/hibernate-core/src/main/java/org/hibernate/annotations/DefaultValue.java
    new file mode 100644
    index 0000000..b3e605e
    --- /dev/null
    +++ b/hibernate-core/src/main/java/org/hibernate/annotations/DefaultValue.java
    @@ -0,0 +1,35 @@
    +package org.hibernate.annotations;
    +
    +import static java.lang.annotation.ElementType.FIELD;
    +import static java.lang.annotation.ElementType.METHOD;
    +import static java.lang.annotation.RetentionPolicy.RUNTIME;
    +
    +import java.lang.annotation.Retention;
    +
    +/**
    + * Specify a default value for the column.
    + *
    + * This is used to generate the auto DDL.
    + *
    + * WARNING: This is not part of JPA 2.0 specification.
    + *
    + * @author 谢继雷
    + */
    +@java.lang.annotation.Target({ FIELD, METHOD })
    +@Retention(RUNTIME)
    +public @interface DefaultValue {
    +
    +    /**
    +     * The default value sql fragment.
    +     *
    +     * For string values, you need to quote the value like 'foo'.
    +     *
    +     * Because different database implementation may use different 
    +     * quoting format, so this is not portable. But for simple values
    +     * like number and strings, this is generally enough for use.
    +     */
    +    String value();
    +
    +}
    diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
    index b289b1e..ac57f1a 100644
    --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
    +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
    @@ -29,6 +29,7 @@ import org.hibernate.AnnotationException;
     import org.hibernate.AssertionFailure;
     import org.hibernate.annotations.ColumnTransformer;
     import org.hibernate.annotations.ColumnTransformers;
    +import org.hibernate.annotations.DefaultValue;
     import org.hibernate.annotations.common.reflection.XProperty;
     import org.hibernate.cfg.annotations.Nullability;
     import org.hibernate.mapping.Column;
    @@ -65,6 +66,7 @@ public class Ejb3Column {
        private String propertyName;
        private boolean unique;
        private boolean nullable = true;
    +   private String defaultValue;
        private String formulaString;
        private Formula formula;
        private Table table;
    @@ -175,7 +177,15 @@ public class Ejb3Column {
            return mappingColumn.isNullable();
        }
    
    -   public Ejb3Column() {
    +   public String getDefaultValue() {
    +        return defaultValue;
    +    }
    +
    +    public void setDefaultValue(String defaultValue) {
    +        this.defaultValue = defaultValue;
    +    }
    +
    +    public Ejb3Column() {
        }
    
        public void bind() {
    @@ -186,7 +196,7 @@ public class Ejb3Column {
            }
            else {
                initMappingColumn(
    -                   logicalColumnName, propertyName, length, precision, scale, nullable, sqlType, unique, true
    +                   logicalColumnName, propertyName, length, precision, scale, nullable, sqlType, unique, defaultValue, true
                );
                log.debug( "Binding column: " + toString());
            }
    @@ -201,6 +211,7 @@ public class Ejb3Column {
                boolean nullable,
                String sqlType,
                boolean unique,
    +           String defaultValue,
                boolean applyNamingStrategy) {
            if ( StringHelper.isNotEmpty( formulaString ) ) {
                this.formula = new Formula();
    @@ -217,6 +228,7 @@ public class Ejb3Column {
                this.mappingColumn.setNullable( nullable );
                this.mappingColumn.setSqlType( sqlType );
                this.mappingColumn.setUnique( unique );
    +           this.mappingColumn.setDefaultValue(defaultValue);
    
                if(writeExpression != null && !writeExpression.matches("[^?]*\\?[^?]*")) {
                    throw new AnnotationException(
    @@ -454,6 +466,11 @@ public class Ejb3Column {
                        else {
                            column.setLogicalColumnName( columnName );
                        }
    +                   DefaultValue _defaultValue = inferredData.getProperty().getAnnotation(DefaultValue.class);
    +                   if (_defaultValue != null) {
    +                       String defaultValue = _defaultValue.value();
    +                       column.setDefaultValue(defaultValue);
    +                   }
    
                        column.setPropertyName(
                                BinderHelper.getRelativePath( propertyHolder, inferredData.getPropertyName() )
    diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java
    index e57636a..3d871f7 100644
    --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java
    +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java
    @@ -423,6 +424,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
                    getMappingColumn() != null ? getMappingColumn().isNullable() : false,
                    referencedColumn.getSqlType(),
                    getMappingColumn() != null ? getMappingColumn().isUnique() : false,
    +               null, // default-value
                    false
            );
            linkWithValue( value );
    @@ -502,6 +504,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
                    getMappingColumn().isNullable(),
                    column.getSqlType(),
                    getMappingColumn().isUnique(),
    +               null, // default-value
                    false //We do copy no strategy here
            );
            linkWithValue( value );
    

    嗯,这是一个仅限hibernate的解决方案 .

  • 1

    JPA和Hibernate注释都不支持默认列值的概念 . 作为此限制的变通方法,请在会话中调用Hibernate save()update() 之前设置所有默认值 . 尽可能接近(缺少Hibernate设置默认值)模仿数据库的行为,该行为在表中保存行时设置默认值 .

    与设置模型类中的默认值不同,这种方法也确保使用 Example 对象作为搜索原型的条件查询将继续像以前一样工作 . 当您在模型类中设置可为空的属性(具有非基本类型的属性)的默认值时,Hibernate按示例查询将不再忽略先前将忽略它的关联列,因为它是null .

  • 6

    如果您使用的是双,则可以使用以下内容:

    @Column(columnDefinition="double precision default '96'")
    
    private Double grolsh;
    

    是的,这是特定于数据库的 .

  • 9

    在插入数据时在数据库中设置默认约束时

    • @Column(columnDefinition='...') 不起作用 .

    • 你需要make insertable = false 并从注释中删除 columnDefinition='...' ,然后数据库将自动从数据库中插入默认值 .

    • 例如当您设置varchar时,性别在数据库中默认是男性 .

    • 你只需要在Hibernate / JPA中添加 insertable = false ,它就可以了 .

  • 1
    @PrePersist
    void preInsert() {
        if (this.dateOfConsent == null)
            this.dateOfConsent = LocalDateTime.now();
        if(this.consentExpiry==null)
            this.consentExpiry = this.dateOfConsent.plusMonths(3);
    }
    

    在我的情况下由于字段是LocalDateTime我使用它,建议由于供应商独立性

  • 0

    这在JPA中是不可能的 .

    以下是使用Column注释可以执行的操作:http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html

  • -2

    您可以在数据库设计器中定义默认值,也可以在创建表时定义默认值 . 例如,在SQL Server中,您可以将Date字段的默认保险库设置为( getDate() ) . 使用列定义中提到的 insertable=false . JPA不会在插入时指定该列,数据库将为您生成值 .

  • 6

    你需要 insertable=false _1487301_注释 . 插入数据库时,JPA将忽略该列,并将使用默认值 .

    看到这个链接:http://mariemjabloun.blogspot.com/2014/03/resolved-set-database-default-value-in.html

相关问题