Suppose I have 3 entities: MonitorPeriod, Monitor and File.
Now the requirement said that for each MonitorPeriod, which table is something like below:
ID, DATE_FROM, DATE_TO
1 <value> <value>
I have One and just One Monitor, which table could be something like below:
ID, COMPILED, ID_PERIOD, ID_FILE
1 true/false 1 1
Now, each File, which table could be something like below:
ID, NAME, DESCRIPTION
1 <value> <value>
could have N Monitor but Just one per MonitorPeriod.
Is there a way to achieve this with JPA Model? The Entity Model should create even the constraint that stop me to insert, inside monitor table, two tuplas with same MonitorPeriod ID and same File ID.
‐‐‐‐----‐-------------‐UPDATE --------------------
Yes Chris you are completely right! Is the same thing I have said to my functional analyst.
The goal is to design this three entity relationship in a way that, in the Monitor table:
ID, NAME, DESCRIPTION, ID_PERIOD, ID_FILE
I will have a constraint that permit me to insert, for example:
ID, NAME, DESCRIPTION, ID_PERIOD, ID_FILE
1 null null 1 1 // ok
2 null null 2 1 // ok
3 null null 1 2 // ok
4 null null 2 2 // should throw error because file 2 is already linked with monitor period 1
I try to repeat the logic: a MonitorPeriod (example from january to march) can have one and just one Monitor (suppose that monitor contains details on file, like size and extensions, thats it). And this is a @OneToOne
relation. Now the problem is that a File can have more monitors but one for period and if a connect File with @OneToMany
relationship with Monitor I can't achieve the situation above.
Maybe I should connect the entity differently or insert another table, I will try up and let you know. In the meanwhile if someone can suggest me some I will appreciate a lot!