Are domain objects the same as JPA entities?
Asked Answered
H

4

8

Are domain objects the same as JPA entities? If I have a value object (e.g. a dollar bill), how can I (or even should I) store that in the datastore as a reference object?

What are the subtleties of domain objects being entities in some cases and value objects in other cases? Could anyone direct me to a good paper on this?

Haystack answered 1/5, 2012 at 7:11 Comment(3)
Yes, domain objects the same as JPA entitiesAmphoteric
They are not if you are in a Domain Driven Design environment, because domain objects tend to be more robust instead of 'anemic' objects. Check #5589641Gerlac
What stops you from putting domain logic in an entity? Isn't that the whole point of mapping table rows to actual objects in the first place? Otherwise, you may as well just use HashMaps to represent the data from a row in table.Oxidate
S
10

"Domain object" is a more conceptual term; "JPA entity" refers to a specific technology useful for implementing domain objects.

Generally domain objects correspond to the nouns (orders, invoices, customers, etc.) in your domain. Usually we see these as being closer to the database rather than pure data transfer objects. So you might see ORM annotations on the classes you use to implement your domain objects, for example.

A lot of people implement domain objects in an anemic way--mostly properties with ORM mappings, but no real logic on the domain objects themselves. They put the logic in domain services.

On the other hand proponents of domain-driven design put the logic on the domain objects.

Either way these are the domain objects in your system.

JPA entities are classes that you annotate with @Entity, @Column, @ManyToOne, etc. This is a way to implement domain objects. You may decide to put domain logic on the objects themselves, as noted above.

Schulein answered 31/1, 2013 at 20:54 Comment(0)
R
0

In the context of Domain Driven Design, they are not the same. A domain object can be an aggregate that contains entities and value objects and should be ignorant of persistence. Thus, it should not contain any JPA annotations.

Rebus answered 10/1, 2018 at 19:25 Comment(0)
C
0

No, Domain Objects are objects with rich behaviour that represents a relevant concept of the business. JPA entities are a technical solution to persistence.

Churinga answered 10/1, 2018 at 19:48 Comment(0)
T
0

Are domain objects the same as JPA entities?

No, they are not.

If I have a value object (e.g. a dollar bill), how can I (or even should I) store that in the datastore as a reference object?

I recommend storing Value Objects using @Embeddable. VO don't need @Id, you should only have reference to them from parent (they are stored in the same TABLE as parent). In case of collections use @ElementCollection.

@Emedded Value Objects performs better in Hibernate:

  • you do not have JOINS if you are not using @OneToOne.
  • Value Object collections can be removed with one DELETE and do not have to be loaded before deletion.

What are the subtleties of domain objects being entities in some cases and value objects in other cases?

Entities are objects that are distinguished by @Id, on contrary Value Objects are distinguished by values. Value Objects are often implemented as immutable, whereas entites/domain-objects contain businness logic that mutate its state.

In some business cases you need representation of Domain Object as Value Object, aka Snapshot, that you for example pass to another Aggregate or publish inside Domain Event.

Could anyone direct me to a good paper on this?

Book: Implementing Domain-Driven Design, Vaughn Vernon.

Transeunt answered 11/1, 2018 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.