-->
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.  [ 5 posts ] 
Author Message
 Post subject: Column always generated by database
PostPosted: Fri Apr 07, 2006 5:46 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
I have triggers in my database that populate a PK column (number), a timestamp column and a date column. In the past, I've used the generated="always" attribute when mapping the timestamp and date properties in the mapping file. Is there an equivalent in annotations?

Also, is there a similar way to do this for the PK column in annotations, where the trigger populates the PK and hibernate knows to pick this up after insertion via annotations?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 5:43 am 
Newbie

Joined: Fri Mar 31, 2006 5:18 am
Posts: 9
Try using the hibernate extensions, like org.hibernate.annotations.GenericGenerator, which gives you access to all of the hibernate pk generators.

e.g. (hint: this is really what you want)

@Id()
@GeneratedValue(generator="trigger-pk")
@GenericGenerator(name="trigger-pk", strategy = "select")
private String id;


Top
 Profile  
 
 Post subject: Trigger
PostPosted: Mon Apr 10, 2006 12:50 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Hey, I gave it a shot. I have the following table:

Code:
CREATE TABLE test_table (
  pkey VARCHAR(30) PRIMARY KEY,
  datecol DATE NOT NULL
);


And a superclass:

Code:
public class Parent
{
  @Temporal( TemporalType.DATE )
  @GeneratedValue( generator="gen_datecol" )
  @GenericGenerator( name="gen_datecol", strategy="select" )
  @Column( name="datecol", nullable=false )
  private Date date;
/*... accessors ... */
}


And the derived class:

Code:
@Entity
@Table( name="test_table" )
public class Test
{
  @Id @Column( name="pkey", length=30 )
  private String pkey;
/*... accessors ... */
}


There are a whole slew of things in this, as I'm not sure if I'm doing things properly with the class hierarchy. When I create a new object of class Test and save it, and commit, the base class's date property doesn't get filled in, and I see the following SQL:

Code:
Hibernate: insert into test_table (pkey) values (?)


I did a little trial and error, but am running out of ideas. Am I annotating the properties correctly? Do I need to add an annotation to the base class declaration (before "public class Parent")?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:09 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Whoops, nm, forgot @MappedSuperclass :). See what desperation can do? It can force you to RTFM!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 12:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'd like to have a @Generated("always") annotation

Patch welcome

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.