-->
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.  [ 12 posts ] 
Author Message
 Post subject: defining constructor for hibernate-injection
PostPosted: Thu Sep 01, 2005 9:11 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
I want to implement status-information in our POJO like isNew or hasChanged.

The remaining open issue is how to make the POJO recognizing when hibernate injects an existing object from db.
Per default hibernate uses default contructor and calls the setters. No chance here set the flag.

Q: How can I tell hibernate to use a given contructor (e.g. passing all props there) instead of the default constructor ?
This way I could set the flag in the POJO.

/m


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 9:20 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
Is it a new or legacy database? If it's a new database, you could add the flag as a column.

Or better yet (and this is off the top of my head), let hibernate use the default constructor, and the application, when first creating a transient object use a different constructor.

When you think about it, even though hibernate uses the default constructor, the flag for any object retrieved from the database would automatically be true... so your default constructor could set it.

And since you'd be creating any transient object, you could just use a constructor where you pass it a value to set the flag false.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 5:21 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
mjstein6300 wrote:
Is it a new or legacy database? If it's a new database, you could add the flag as a column.

Or better yet (and this is off the top of my head), let hibernate use the default constructor, and the application, when first creating a transient object use a different constructor.

When you think about it, even though hibernate uses the default constructor, the flag for any object retrieved from the database would automatically be true... so your default constructor could set it.

And since you'd be creating any transient object, you could just use a constructor where you pass it a value to set the flag false.


theoretically this would work, but can hardly enforce that application developers don't use the default constructor. They will use it.

There is no other way but telling hibernate to use a given special constructor.

How to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 5:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Interceptor.instantiate

and create it with a constructor that sets your flag...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 10:29 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
ok, great. but what do I do with the given Serializable id ?
If I ignore it it results in trouble with id-idfentification.



max wrote:
Interceptor.instantiate

and create it with a constructor that sets your flag...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you put it in your id field

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 7:17 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
err - good point, but what if there is a composite-id ?
What I need is a hibernate service-method that injects the Serializable id into the newly created POJO.

max wrote:
you put it in your id field


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 7:52 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
also, in case the key is identical with a property, the name of the property differs.
Hence the id can only be set using the info from the mapping-file. Hibernate should do the job. Just how?

axismundi wrote:
err - good point, but what if there is a composite-id ?
What I need is a hibernate service-method that injects the Serializable id into the newly created POJO.

max wrote:
you put it in your id field


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 7:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you know which constructor to call, why don't you know which property is the id ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 7:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
..and anyhow look at ClassMetaData.setIdentifier()

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 11:04 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
thx, good hint. however this appears to work only using a static accessor to hibernate-session-factory.

max wrote:
..and anyhow look at ClassMetaData.setIdentifier()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 05, 2005 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh -why is that a problem ?

you just call a method on your interceptor that can keep it around for you.

_________________
Max
Don't forget to rate


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