0

I'm trying to create an ecommerce website using spring, hibernate and for the first time following DDD.

At the moment the situation is this. I'm considering the object USER as aggregate root which has a list of addresses (previous ones.. and current) and for every address there is associated a Country (isocode, name)..

I assumed that Address and Country are Value Objects.. strictly related to the user.

In the @Entity class User there is: @ElementCollection @CollectionTable(name = "addresses", joinColumns = @JoinColumn(name = "address_id")) @OrderColumn(name="user_id") private List addresses;

In the @Embeddable class Address I would like to create a separate table for Country that I can populate during the initialization and the Address has only a foreign key.

I tried @Embeddable Country and in Address @Embedded private Country country

and also @SecondaryTable .. but this can be used only in @Entity class.. So the problem is that the fields of Country are saved inside the Address Table.

SHould I create an @Entity for Country? Or there is another way to map these class?

Thanks

MattDavey
  • 8,897
  • 3
  • 31
  • 54
Gfalco
  • 77
  • 3
  • 13
  • 1
    I don't usually like to make absolute statements, but I'll make an exception in this case.... **You absolutely, definitely should NOT let the database schema have ANY influence over the design of your domain model.** DDD 101. – MattDavey Jun 18 '13 at 17:00

1 Answers1

0

I really don't see the point of making Country an embedded object. You'll repeat the same codes and names again and again for all your users, instead of just make them all point to the same countries. Just have a table with all the countries, mapped by an entity, and make all the addresses refer have a ManyToOne association with Country.

BTW, if you want to design a UI where you have to enter an address, I guess that you'll need a select box displaying al the countries to choose from: you won't enter the code and name of the country manually each time. So you'll need a Country entity and a findAll method to get them all and populate your select box.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255