-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Persisting entities with database-level default values
PostPosted: Mon Jan 28, 2008 5:19 am 
Newbie

Joined: Mon Jan 28, 2008 5:12 am
Posts: 1
Hi.

I'm using Hibernate as a JPA implementation in a Java SE context
and a MySQL database.
I ran into a problem when I tried to persist entities where some
of their properties have default values at the database level.

To illustrate my problem, here is a simple example.
Let's say we have the following DDL
Code:
  CREATE TABLE `Deletions` (
    `id` int(11) NOT NULL auto_increment,
    `deleted` datetime NOT NULL default '0000-00-00 00:00:00',
    PRIMARY KEY  (`ID`)
  );

and the corresponding entity

Code:
  public class Deletions {

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private int id;

      private Date deleted;
   
      public Deletions() {
      }

      public int getId() {
          return id;
      }

      public void setId(int id) {
          this.id = id;
      }

      public Date getDeleted() {
          return deleted;
      }

      public void setDeleted(Date deleted) {
          this.deleted = deleted;
      }
   
  }


When I try to persist a new entity in a transaction like this

Deletions d = new Deletions();
em.persist(d);

I get

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'deleted' cannot be null

I understand why this happens: When Hibernate tries to persist
the entity the property deleted is null. But the DDL says column
deleted must not be null.

Is their a way to tell Hibernate to not include uninitialized properties
in insert-statements or a way to specify default values for such
properties?
Right now I'm setting some properties to their default values in the
no-arg constructor, but I'm looking for a better solution.


Best regards,
Achim


Top
 Profile  
 
 Post subject: Re: Persisting entities with database-level default values
PostPosted: Mon Mar 10, 2008 3:19 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
In a normal mapping, you would have to activate dynamic-insert for the class. Without this setting, the insert command contains all columns. With dynamic-insert, the command is generated for every single insert and contains only non-null values.

There is one problem though: I haven't found a way to tell Hibernate to reselect those columns yet. Therefore, I manually reload the object after inserting.

hth,
Heinz


Top
 Profile  
 
 Post subject: Re: Persisting entities with database-level default values
PostPosted: Fri Mar 14, 2008 3:00 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
hhuber wrote:
There is one problem though: I haven't found a way to tell Hibernate to reselect those columns yet. Therefore, I manually reload the object after inserting.


I've found a way by now. And I've posted a patch: HHH-3180


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.