1

I am reading about DDD and I have learned that Value Object is immutable, if you want to change it, you will have to create a new one. I have just read the information on How are Value Objects stored in the database? , it works well for Address class and I also read https://cargotracker.java.net/ and https://gojko.net/2009/09/30/ddd-and-relational-databases-the-value-object-dilemma/. But I want to do something different .

I am working on a billing system , it has 4 tables/classes
TPerson - fields: id_person, name -> <<Entity>>
TMobile - fields: id_mobile, number -> <<Entity>>
TPeriod - fields: id_period, id_person, id_mobile, begin_date, end_date -> <<Value Object>> (I think, because the dates can be change)
TCall - field: id_call, id_period, etc... -> <<Value Object>>

The table TCall has many records, if I change the period record dates (Value Object, table TPeriod) it will create another Object Period then id_period will change(delete, insert a record) , but the foreign key in table TCall will be violated. How Could I implement the period class ? if i implement as a value object , it will be immutable and turns out I will not be able to change anything whatsoever.

Thanks, Fernando

Community
  • 1
  • 1
Fernando
  • 11
  • 2
  • A short answer - if you have (need) identifiers (e.g. `id_period`, `id_call`) then it is not a Value Object (VO) it is an Entity. If identifiers are needed in order to have FK then ... try to create domain model without thinking about persistence, FK dependencies, DB tables. – Ilya Palkin Aug 11 '16 at 11:22

2 Answers2

0

if it's a value object you don't have a period table/id.

A value object is just a grouping of certain fields. For example a call might have a start time, an end time, and then you could create a Duration Value object with starttime and end time from the call table. In your java code it would be then more convenient to talk about the call duration instead of the start/end time separately.

However, it certainly could make sense to make period an entity, but then period 201601 probally always have the same start/end time and you wouldn't need to make changes to it. And if you did you make changes to the entity directly and keeping the ids in tact.

Batavia
  • 2,497
  • 14
  • 16
0

Thank for your help,

I have this situation:

TPerson - fields: id_person = 1 , name = "John" TMobile - fields: id_mobile = 100, number "555-0123" TPeriod - fields: id_period = 1000, id_person = 1 , id_mobile = 1, begin_date = "2016-01-01", end_date = "2049-12-31" TCall - field: id_call = 1, id_period = 1000

The period is a relation between TPerson and TPeriod, in this example John has a mobile between "2016-01-01" and "2049-12-31". On the table TCall there are John's calls record, but if i replace the period (TPeriod table) end_date to "2016-02-01", from my understanding the end_date will be inconsistent, it turns out i cann't replace because it's a value object, not a entity. I considered to implement like this.

// Create a class DatePeriod
 public class DatePeriod {
    private final begin_date;
    private final end_date;
    DatePeriod() {}
    public static DatePeriod of(Date begin_date, Date end_date) {
        this.begin_date = begin_date;
         this.end_date = end_date;
    }
    // implement equals / hashcode...
}

// Period class
public class Period {
    int id;
    // others mappings id_person / id_mobile
    DatePeriod datePeriod;
}

Still, i will have to update datePeriod attribute

Thank you for your attention to this matter

Fernando
  • 11
  • 2