4

I have domain model developed based on DDD concept, pretty much Object Oriented and have both state and behaviors in it. The problem is in order to use Hibernate all the persisting attributes has to have getters and setters. This is not appealing since I don't want to introduce setter for some attributes of my domain objects. Should I map my domain object to DTO instead, which sole purpose is to have maintain just the data.

Thurein
  • 6,645
  • 5
  • 22
  • 23
  • Similar question here http://stackoverflow.com/questions/2676689/does-hibernate-always-need-a-setter-when-there-is-a-getter – Andrew Fielden Dec 16 '11 at 14:53

1 Answers1

3

In general, the two concepts are the same. A domain model is used to describe the model of your objects from the viewpoint of the problem domain (ie. the information used to solve a particular problem or set of problems) and an entity model is used to describe the model of your objects from the viewpoint of a system of actors (in many cases, this is some application that uses the model to solve problems and acts on the entities).

So, in general, they are the same thing.

That said, Hibernate is very flexible and in general, doesn't require you to do much of anything with your persisted objects structure. The key is all in how you define the mappings. In any case, I would not suggest having a DTO just to deal with persisting the data. Hibernate does that all internally using proxies...that's it's job. Adding more classes just adds to the complexity of your application and doesn't really provide much benefit. More complexity is almost never a good thing.

With Hibernate, you can have private setters, or have Hibernate only operate on the fields and ignore the getters/setters completely. In the first case, you are still introducing a setter, but it is private and so it doesn't impact the public API of the class. With field-access, Hibernate doesn't need there to be any getter or setter for a property, but it also goes around any logic you may have had to do other things, like set transient (non-persisted) properties on your objects.

Have a read through the Hibernate manual, especially on the section about mapping. It is a really flexible ORM that doesn't restrict you any more than it has to.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • Hibernate will use some proxy to access private setters? – Thurein Dec 16 '11 at 14:54
  • Yes. The proxies Hibernate uses give it all the access it needs to get at private fields, private constructors, and private setters. Like I said, have a read through the user guide. It lays out all the options in great detail and has lots of good examples of how to map things the way _you_ want. – cdeszaq Dec 16 '11 at 14:56