-->
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: How to use a Session in a persistent class Constructor ?
PostPosted: Fri Aug 29, 2003 9:12 am 
Newbie

Joined: Fri Aug 29, 2003 7:49 am
Posts: 3
Location: Paris
I have a class Account persisted by Hibernate. I want to use an Hibernate Session in the default constructor of class Account, in order to load complementary objects.

But it doesn't work because when Hibernate creates the SessionFactory, Hibernate tries to create an instance of Account by calling the default constructor. As the SessionFactory is not already created, the default constructor can't open a Session and raises an exception...

So, I have two questions:

1) Why Hibernate needs to create an instance of each persisted class during the creation of a SessionFactory?

2) Is it possible to create a SessionFactory without this behavior?

Thank you for your answers !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 9:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why in gods name do you need a Session in your constructor? This is a very bad design decision.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 9:58 am 
Newbie

Joined: Fri Aug 29, 2003 7:49 am
Posts: 3
Location: Paris
The Account class has a "category" property. This property can take one value in a list of several possible values.

I want to initialize a "categoryList" property in the default constructor, in order to provide a "getCategoryList()" method returning the list of all possible values for the "category" property.

I "feel" this is a bad design decision, but I don't really understand why. Can you explain your point of view.

Out of curiosity, I'm already interested to know why Hibernate needs to create an instance of each persisted class during creation of a SessionFactory.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 10:24 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use the "typesafe enumeration" pattern for this.

Read this book: http://developer.java.sun.com/developer ... ctivejava/

Chapter 5, Item 21, it is available online. Hibernate directly supports the mapping of enumeration classes, see the Hibernate documentation, "persistent enumerations".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 10:50 am 
Newbie

Joined: Fri Aug 29, 2003 7:49 am
Posts: 3
Location: Paris
This a good idea, but the property "categories" holding the list of possible values for the property "category" is not constant. In fact, the categories are stored in the databse and persisted with a Category class. So, the property "categories" is just a kind of shortcut to find all instances of Category class.

I chose this design to have an "all-in-one" model (the Account class) that can be used in my view (a Swing panel) with a getCategory() to retrieve the current value and a getCategories() to retrieve list of possible values.


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.