-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to map a singleton ?
PostPosted: Thu Nov 23, 2006 5:57 am 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Hi there,

I'm facing a little problem when trying to map a singleton using Hibernate.
In fact, there's no way to specify a static method (like "GetInstance") to use, as Hibernate always tries to call the default entity class constructor.
Is there any solution to get this working ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:54 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, you can not map a singleton (why should you?)

a persisted object must be associated with just a single session, and think how you can do this using a singleton?

and do you want to have just a single record in your table?

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:56 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, you can not map a singleton (why should you?)

a persisted object must be associated with just a single session, and think how you can do this using a singleton?

and do you want to have just a single record in your table?

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:58 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, you can not map a singleton (why should you?)

a persisted object must be associated with just a single session, and think how you can do this using a singleton?

and do you want to have just a single record in your table?

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:58 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
mmm, you can not map a singleton (why should you?)

a persisted object must be associated with just a single session, and think how you can do this using a singleton?

and do you want to have just a single record in your table?

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject: Re: How to map a singleton ?
PostPosted: Thu Nov 23, 2006 7:08 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
djflex68 wrote:
Hi there,

I'm facing a little problem when trying to map a singleton using Hibernate.
In fact, there's no way to specify a static method (like "GetInstance") to use, as Hibernate always tries to call the default entity class constructor.
Is there any solution to get this working ?

Thanks.


It's not possible to ask Hibernate to use getInstance(). I guess what you have is a special one-line table containing your "singleton" object data, isn't it ?

If so, I think you should have a look at second-level cache. In fact, having a persistent instance never or very rarely modified (is it your case ?) is typically a use case where to use second-level cache.

If your singleton is going to be often modified, then second-level cache is not the solution. But before going further, please precise what you do so that we can help more usefully :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 8:49 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
I've written a generic UserType, together with some helpers it can solve this problem.
http://www.codeproject.com/useritems/SingletonUtil.asp


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 7:38 pm 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
hello,

Thank you very much for these replies.
Quote:
it's not possible to ask Hibernate to use getInstance(). I guess what you have is a special one-line table containing your "singleton" object data, isn't it ?

Yeah that could be a part of the solution. In fact, this singleton represents a parameter table in the database. This means I just need one instance of this class. But then I would have to design a table with only one line and only one colum (ID)... Not really meaningful...
Quote:
I've written a generic UserType, together with some helpers it can solve this problem.
http://www.codeproject.com/useritems/SingletonUtil.asp

I will look at this, it seems to be very interesting.

By the way I have a question which seems to relate to the same topic :
If I have a class which doesn't contain any attribute, it justs contains one or many persistant lists to other classes. Then I will have a table with only one ID column in the database, which is not really meaningful too...
Is it possible to avoid this ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 25, 2006 7:03 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
djflex68 wrote:
By the way I have a question which seems to relate to the same topic :
If I have a class which doesn't contain any attribute, it justs contains one or many persistant lists to other classes. Then I will have a table with only one ID column in the database, which is not really meaningful too...
Is it possible to avoid this ?


You mean you've got a class that would have a one-to-many relationship with some others ? If so, yes, you'd have only one id. But if many-to-one, then you would be forced to have some foreign keys in this table.

Well, about avoiding this, it's not really possible to answer without having more infos on why you'd want to create this "central" object knowing some others.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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