6 Replies Latest reply on Apr 13, 2007 4:25 AM by oskar.carlstedt

    NOT NULL columns with a DEFAULT

      Hello,

      I have a column in my DB (PostgreSQL) that is NOT NULL with a DEFAULT value:

      foo float8 NOT NULL DEFAULT 1
      


      I have an entity:

      @Column(name="foo", unique=false, nullable=true, insertable=true, updatable=true)
       public Double getFoo() {
       return foo;
       }
       public void setFoo( Double foo ) {
       this.foo = foo;
       }
      


      I want to be able to create this entity without specifying "foo", and persist it so that the DEFAULT value is used. If I do not set the foo property on my entity, I get the following exception:


      -ERROR: null value in column "foo" violates not-null constraint


      If I modify the @Column annotation, setting nullable = false, I get a Hibernate error.:


      org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mypackage.impl.Node.foo


      How can I create an entity that, when persisted, will cause the DB to use the DEFAULT value for the "foo" field?

        • 1. Re: NOT NULL columns with a DEFAULT
          oskar.carlstedt

          I have the same problem. Did you find any answer/solution for this?

          Kind regards
          Oskar

          • 2. Re: NOT NULL columns with a DEFAULT
            uagardx

             

            "oskar.carlstedt" wrote:
            I have the same problem. Did you find any answer/solution for this?

            Kind regards
            Oskar


            • 3. Re: NOT NULL columns with a DEFAULT


              Not sure if this is what you want: You can simply set a default value on the java side, either in the constructor or using the factory pattern.

              As far as I know there is no JPA way to set a default value.

              Regards

              Felix

              • 4. Re: NOT NULL columns with a DEFAULT
                oskar.carlstedt

                Hi!!

                No It is not exactly what I want. I solved everything by using inheritance and the SINGE_TABLE inheritance method. Then I could skip my property I didn't want to include.

                But originally, I have a database specifying the default value. This value is used by other applications too. So to centralize the default value, we declared it in the database - Therefore, I don't want to set the default value in my implementation. Maybe this is something to report as a new annotation for ejb3. I don't think I'm the only one that is modeling a persistence layer on an existing database.

                Anyway, I resolved my problem by using inheritance.

                @Entity
                @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
                @DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
                @Table(name = "THE_TABLE")
                public class GenericEntityBean implements Serializable {
                
                 @Id
                 @Column(name = "id")
                 @GeneratedValue(strategy = GenerationType.IDENTITY)
                 protected Integer id;
                
                 @Column(name = "COMMON_VALUE")
                 protected String commonValue;
                
                 ...
                }
                
                
                @Entity
                @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
                @DiscriminatorColumn(name = "discriminator")
                @DiscriminatorValue(name = "SPECIFIC")
                public class SpecificEntityBean extends GenericEntityBean implements Serializable {
                
                
                 @Column(name = "ANOTHER_SPECIFIC_VALUE")
                 protected String anotherSpecificValue;
                
                 ...
                }
                
                


                Best Regards
                Oskar


                • 5. Re: NOT NULL columns with a DEFAULT

                  I don't see how that soved your problem.

                  Regards

                  Felix

                  • 6. Re: NOT NULL columns with a DEFAULT
                    oskar.carlstedt

                    Hi!!

                    Dtabase columns that are not part of the used implementation (not mapped by properties in the class) are not exposed in the SQL statement to the database - which is exactly what I want (the database shall set this value, not the sql statement).

                    Regards
                    Oskar