• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to apply creational patterns when working with frameworks like hibernate

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop using Hibernate.
 
Deriko Prata
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see now that refactoring will only add complexity and not really improve that much my design.

Thanks for the advice.

reply
    Bookmark Topic Watch Topic
  • New Topic