-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate DAO based design question
PostPosted: Wed Apr 16, 2014 4:32 am 
Newbie

Joined: Wed Apr 16, 2014 4:28 am
Posts: 1
I’m currently working on my first hibernate project an ran into some design issues.
This project involves refactoring a lot of old api into a 3 layer api:
* The “management” layer which contains all the business logic
* The “DAO” layer which contains all database calls
* The actual “Entities” returned by hibernate, contain setter, getter, add, remove methods but no business logic whatsoever.

But now I have a question about this design. The “Entity” layer should not contain any business logic, this should be present in the management layer only.
But now every time the code wants to use a certain getter/setter the user should use a manager object. This would make the use of this API quite clunky.

So then I started moving into the direction of using “wrapper objects”. These objects contain both the Entity and the “Manager” and encapsulate the setters/getters so these “wrapper objects” only contain final methods that just call setters & getters for the entity using the manager.

So far so good, I had to (ab)use the “ResultTransformer” when querying in hibernate to transform the objects from Entities to wrapper objects but that went all right.
But now I’m stuck on the fact that I also need to “transform” the @OneToOne & @OneToMany, …. variables since these will still return the database entities instead of my wrapper objects.

I believe I’m missing something somewhere, it feels like such a common problem where you don’t want your Entities to contain business logic but still keep your api easy to use. (Not requiring another object for every setter/getter one wishes to call).


Top
 Profile  
 
 Post subject: Re: Hibernate DAO based design question
PostPosted: Thu Apr 17, 2014 10:41 am 
Newbie

Joined: Mon Apr 07, 2014 10:20 am
Posts: 12
If I understand you correctly, you're describing exactly the same sort of pattern I have wanted to try as well. You start by adding one or two "friendly helper" methods to an entity, but when a system starts to grow large enough and touched by enough people those entities start to become utterly littered with business logic. You want to alleviate this by separating the entities from the surrounding business logic.

So, if you're describing the same sort of problem, then you want some form of architecture like this, I assume?
Code:
// Your "business logic wrapper"
class Item {
   private ItemData itemData;
   public Item(ItemData itemData) {
      this.itemData = itemData;
   }

   // any relevant business logic for the Item class
}

// Your actual entity with simple getters/setters for data
class ItemData {
   @Id
   @Column(name = "item_number")
   private String itemNumber;

   @Column(name = "item_description")
   private String itemDescription;

   // just your standard getters/setters for modifications to persisted data
}

// And some form of DAO to read in Items from the database
class ItemDao {
   public Item read(String itemNumber) {
      ItemData itemData = session.read(ItemData.class, itemNumber);
      return new Item(itemData);
}


However, your trouble is that when you have mappings between entities the wrappers will not be returned on the getter calls, correct?

I have thought about this problem and perhaps there is some form of Hibernate hook out there that I am unaware of. If so, then perhaps somebody a bit more knowledgeable than me will chime in here. However, another method I have personally considered to solve this problem that *seems* like it should be relatively straightforward is to abuse the @Embedded property in Hibernate.

In other words, do the above like this:

Code:
// Your "business logic wrapper"
class Item {
   @Embedded
   private ItemData itemData;

   // any relevant business logic for the Item class
}

// Your actual entity with simple getters/setters for data
@Embeddable
class ItemData {
   @Id
   @Column(name = "item_number")
   private String itemNumber;

   @Column(name = "item_description")
   private String itemDescription;

   // just your standard getters/setters for modifications to persisted data
}

// And some form of DAO to read in Items from the database
class ItemDao {
   public Item read(String itemNumber) {
      Item item = session.read(Item.class, itemNumber);
      return item;
}


This seems to have the added bonus of your "business logic" classes being seen as actual entities by Hibernate (which can be directly used in HQL) while still allowing for a proper logical separation between your business logic and your data accessors.

Again, this is purely academic in my head; I haven't had the opportunity to try this out in a large system so there very well could be problems I have not thought of. Still, if it works for you then please keep me updated!


Top
 Profile  
 
 Post subject: Re: Hibernate DAO based design question
PostPosted: Thu Apr 17, 2014 11:24 am 
Newbie

Joined: Thu Apr 17, 2014 10:52 am
Posts: 1
Hi,

My understanding of your design is as follows:
1. Interface XXXManager:
a. Has an abastract method doSomething()

2. Class XXXManagerImpl:
a. Implements XXXManager i.e. method doSomething()
b. Has XXXDao attribute
c. Method doSomething gets from Dao and uses the same:
XXXModel xxxModel = XXXDao.getSomething();
//Logic using xxxModel.getAttribute1() etc. ...

3. XXXDao
a. Has an abastract method getSomething()

4. XXXDaoImpl:
a. Implements XXXDao
b. Returns Model (Entity)

5. Model (Entity):
a. It is a POJO having mapping annotations

Is there any other layer which is using Manager layer ?

I am sorry but I am not sure what do you mean by "But now every time the code wants to use a certain getter/setter the user should use a manager object. This would make the use of this API quite clunky". Could you please elaborate ?

By the way I am not in favour of using wrapper object unless there is some exceptional case because it will be an overhead and the purpose of using Hibernate would be defeated.

Cheers


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