wood burning stoves*
The moose likes OO, Patterns, UML and Refactoring and the fly likes How to apply creational patterns when working with frameworks like hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "How to apply creational patterns when working with frameworks like hibernate" Watch "How to apply creational patterns when working with frameworks like hibernate" New topic
Author

How to apply creational patterns when working with frameworks like hibernate

Deriko Prata
Greenhorn

Joined: Dec 16, 2010
Posts: 21
Hi there, I have a class in my domain model that has too many constructors, thus it became a nuisance instantiating this class. I´m refactoring it applying the builder pattern. As it is an entity class, hibernate needs a non private constructor to instantiate it by reflection, but i don't want it being instantiated any other way than by the builder. What approach can i take?

Thanks in advance, regards
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Stop using Hibernate.
Deriko Prata
Greenhorn

Joined: Dec 16, 2010
Posts: 21
Hi, thanks for the fast response. This is not only a hibernate problem, every framework that needs to call Class.newInstance() will need a non private constructor. For instance, in my case, I´m working in a project integrating java and flex with BlazeDs and it has the same problem. I came up with the solution of copying my domain model class state to a DTO with public constructor , but I didn´t want to use that approach. Is there any other solution?


Thanks again for the response.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Using a framework to create software is an "option", it is not a requirement. If something does not fit well with your programming style, then don't use it and write your own code instead.
Deriko Prata
Greenhorn

Joined: Dec 16, 2010
Posts: 21
Hi , I understand that when you are starting a project you can balance how much using certain frameworks will impact in your design. But in my case I´m working with legacy code and trying to improve it. It will be a poor design choice if I let the public constructor in my domain class just for the frameworks, but document not to use it, plus log warning messages when someone try to instantiate the class by the public constructor and not by the builder ?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
If you have a specific requirement that you are trying to improve in an existing application, then you should be working along with what exists and try to find "compatible" designs that work with what you have. It will be "poor" design if you create a so-called "improvement" and it is not compatible with the existing application. For some individuals, this will take a significant amount of discipline and self-control.

So far, it sounds like your design concept is not compatible and will conflict with what exists today.

Aside, if the existing class has many constructors, in order to handle requirements, and you redesign it's constructors to work with a single argument, you are only moving the logic to the code that instantiates the instance of this class. Moving the logic that called a specific constructor to logic that creats your parameter object (or your Builder) is not much of a change or improvement and may not be worth the headache.
Deriko Prata
Greenhorn

Joined: Dec 16, 2010
Posts: 21
I see now that refactoring will only add complexity and not really improve that much my design.

Thanks for the advice.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to apply creational patterns when working with frameworks like hibernate
 
Similar Threads
field intialization
WA #2 ..... word association
warning includeantruntime was not set
how to extend a static builder
Eclipse Indigo - Unable to start Window Builder