-->
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.  [ 10 posts ] 
Author Message
 Post subject: Factory method instead of constructor.
PostPosted: Mon May 01, 2006 6:22 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
Hibernate version: 3.1/3.2RC1

I have column in database which contains limited set of values. But they aren't continuous (like: 1, 2, 6, 7, 10, etc) so I couldn't use enums. I don't want to create new object for these. I want to use:

Code:
Object valueOf(idClass id) {...}

method to create them. Can this be done with hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 7:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
entities: Interceptor.instantiate (but returning the exact same instance is definitly not recommended for mutable entities)

values: usertype's

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 7:50 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
It is immutable. It's just a enum-like entity.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 8:08 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
I think proposed solution won't help me. I need something like:

Code:
@Enumerated(value=EnumType.ORDINAL)


But where I can specify method which creates object. I mean I need to create object when Hibernate loads column which contains integer it should call valueOf(...). Note that object I need to create isn't registered in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 8:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
did you not read my first answer ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 8:49 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
Ok. I read it. But interceptor invokes this method only for creating entities registered with hibernate. Not for entity columns objects creation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 8:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
huh ? what is the difference ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 8:56 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
Not sure what you mean. Can you explain more clearly?

What I mean is that Hibernate should set property like this:

Code:
setProp(PropClass.valueOf(propId))


Sorry if I explained it purely.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 9:00 am 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
Sorry. What I ment is: entity.property = PropClass.valueOf(propId); I need this to set properties which don't have setters. Valuable when property cannot be edited.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 9:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so, use Interceptor.instantiate to call that method.
Hibernate calls instantiate before it will assign the value.

if it is just a value use a usertype where you have even more control over creation.

_________________
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.  [ 10 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.