-->
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.  [ 8 posts ] 
Author Message
 Post subject: Using @Version in the base class
PostPosted: Mon Jun 23, 2008 6:31 pm 
Newbie

Joined: Mon Sep 24, 2007 8:11 am
Posts: 16
Hibernate version:3

I tried to add a version property to my base class using the javax.persistence.Version annotation:

public abstract class BaseManagedObject implements Serializable
{

@Version
@Column(name="revision" )
protected Long revision;

/**
* Get the Hibernate version for this managed object
* @return the object's version
*/
public Long getRevision()
{
return revision;
}


But when I updated an object that extends this base class, the version field was never updated.
However, when I moved this property into the extended class, the version field got updated as expected:

@Entity
@GeneratedEntity
@Table(name = "User_Table")
public class User extends BaseManagedObject
{
@Version
@Column(name="revision" )
private Long revision;

/**
* Get the Hibernate version for this managed object
* @return the object's version
*/
public Long getRevision()
{
return revision;
}


I did not annotate the base class with @Entity, since I did not want to create a table for it. In fact, when I added @Entity and @Table annotations, I got AnnotationException from the SessionFactory at startup.

Thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 5:44 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi ped,

indeed you need not to annotate your base class as Entity, but just do this
Code:
@MappedSuperclass
public abstract class BaseManagedObject implements Serializable

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject: MappedSuperclass did not work
PostPosted: Tue Jun 24, 2008 7:57 am 
Newbie

Joined: Mon Sep 24, 2007 8:11 am
Posts: 16
Thanks for the reply Carlo -
I replaced the @Entity annotation with @MappedSuperclass, but I got the same result - the version field is not intialized at create time or updated when the object is updated.


Top
 Profile  
 
 Post subject: Re: MappedSuperclass did not work
PostPosted: Tue Jun 24, 2008 8:17 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
ped wrote:
I replaced the @Entity annotation with @MappedSuperclass, but I got the same result - the version field is not intialized at create time or updated when the object is updated.


Hi ped,

maybe the problem is due to the fact that you have a duplicate version attribute - in the base class and in the derived class. Remove it from the derived class.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 10:00 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Image
Here's a very simple tutorial that demonstrates how to map inheritance with Hibernate3 and JPA annotations. It's very simple, and it might give you a good idea on where to add the appropriate annotations:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=16mappinginheritancewithjpa

Image

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: MappedSuperclass did not work
PostPosted: Tue Jun 24, 2008 12:28 pm 
Newbie

Joined: Mon Sep 24, 2007 8:11 am
Posts: 16
carlolf wrote:
ped wrote:
I replaced the @Entity annotation with @MappedSuperclass, but I got the same result - the version field is not intialized at create time or updated when the object is updated.


Hi ped,

maybe the problem is due to the fact that you have a duplicate version attribute - in the base class and in the derived class. Remove it from the derived class.


I never ran with the version attribute defined in both classes. I was trying to show how I defined and annotated the attribute in each class, when it was declared in the class.

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 24, 2008 5:50 pm 
Newbie

Joined: Mon Sep 24, 2007 8:11 am
Posts: 16
I discovered the problem - tho I don't think this should be an issue...

When I place the @Version and @Column annotation to the field declaration in my base class, the extended class has its version column updated correctly.
When I place the annotations on the getter method for the version column in the base class, the extended class does NOT get its version column updated.

I believe that @Version is valid for both methods and for fields. It seems to only work for the field in this case.

Thanks for the help,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 6:49 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
actually it's your @Id annotation that determines if it looks only at field level annotation or only at getter level annotations

_________________
http://www.ohloh.net/accounts/ge0ffrey


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