Create an entity inside an entity: how to persist?

You Order class needs to have a reference to a Product, that could be null. When you call setCompleted you can create a new Product instance and assign it. Something like.

You Order class needs to have a reference to a Product, that could be null. When you call setCompleted you can create a new Product instance and assign it. Something like public void setCompleted(){ ... this.

Product = new Product(...); ... } then depending on your mapping (check your ORM documentation for this) you will have to call the entiity manager save method on both order and product, or just order (if the relation owner is Order and cascade is enabled). As this is business logic, I would not hide it in the model itself but rather have in the service layer.

– Sander Marechal Dec 8 '11 at 10:29 business logic can(should) go on the domain objects, martinfowler.com/bliki/AnemicDomainModel... – NimChimpsky Dec 8 '11 at 10:33 its not "hidden in the model itself" at all. Its the opposite, its available everywhere the domain object is and encapsulated so that no other knowledge is required. – NimChimpsky Dec 8 '11 at 10:43 I see your point, it's really a matter of preference.

Some more reasing here stackoverflow.com/questions/110328/… When using ORMs, I never put business logic in domain objects cause I want to have all the code creating/deleting objects/relations in the same place... – mericano1 Dec 8 '11 at 10:51 Your link points to DTO's/value objects, not domain objects. I think there is a generally accepted best practice, and that is sticking behaviour on domain objects keeping everything as object oriented as possible. – NimChimpsky Dec 8 '11 at 11:06.

I would have a new Product created as part of the setCompleted method. So that everytime Product is persisted any mapped entities also get persisted. Just let hibernate do the leg work ... do not explicitly persist it yrself using the service layer.

For example within Order (assuming product is declared properly as a entity): @OneToOne(cascade = CascadeType. PERSIST) @JoinColumn(name="product_fk",nullable = false) private Product product; public void setCompleted(){ setProduct(new Product(...)); } DDD with hibernate.

You should create that new product from your service layer. E.g. You could have an OrderService that pulls in (talks to) your Order Repository and Product Repository.

The when you call SetCompleted() you also create the product (via the Product Repository). You could also look and the Pipes and Filters pattern and create an Order Pipeline. That way you can plugin various steps (filters) that execute one after the other.

You should not create a product from within an Order entity because your domain entities should be database agnostic. You need a repository to create a new product, so the service layer facilitates this by talking to the data access layer.

1 also you want to use factories for creating of new entities (ProductFactory) and factory can be injected into OrderService and does it job when setComplete() is called. That way it is also easy to mock and unit test. – Tomas Dermisek Dec 13 '11 at 4:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions