-->
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.  [ 1 post ] 
Author Message
 Post subject: Generating an object property value via SQL during insert.
PostPosted: Thu Mar 20, 2008 6:53 pm 
Newbie

Joined: Thu Mar 20, 2008 6:14 pm
Posts: 3
Location: Berkeley, CA
Hi all,

I'm trying to generate and assign (via SQL query) a default value to an object property during the object's initial save to the database. I noticed that the Hibernate column mapping element has a 'default' attribute that lets you attach an "SQL expression" - can this be used to assign a default value to a property?

I made a test object & mapping to play with but haven't had much luck so far. Here's what I've created:


Object Code:
Code:
public class Obj{
   private int id;
   private int myValue;
   
   public Obj(){
   }
   
   public int getId() {
      return id;
   }

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

   public int getMyValue() {
      return myValue;
   }

   public void setMyValue(int value) {
      this.myValue = value;
   }
}


Hibernate Mapping File
Code:
<hibernate-mapping>
   <class name="myClass.Obj" table="obj">
      <id name="id" column="id" unsaved-value="undefined">
         <generator class="sequence">
            <param name="sequence">obj_id_seq</param>
         </generator>
      </id>      
      <property name="myValue">
         <column name="myvalue" not-null="true" default="select max(id) from obj;"/>
      </property>
   </class>
</hibernate-mapping>


Obj Table in the database:
Code:

                        Table "public.obj"
Column   |  Type   |                    Modifiers                     
----------+---------+--------------------------------------------------
id       | integer | not null default nextval('obj_id_seq'::regclass)
myvalue  | integer | not null


So when I create a new object, assign no value to the property 'value', and save that object, Hibernate inserts and saves '0' as the default value for the property. What I really want it to do is insert the value that's the result of the SQL query in the default attribute of column. Something like:

Code:
insert into obj (myvalue) values ( (select max(id) from obj) );


Is that what the default attribute of the column element is for? If not, any suggestions that would allow me to do something similar? I'm looking for such a solution because can't assign a default value to the column in the database.

Many thanks for any suggestions or help,
-Rob-


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

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.