1

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!

CoderJammer
  • 599
  • 3
  • 8
  • 27
  • it isn't clear from your description the relationships - What would your java object model look like? Start with that, then work on the JPA/DB schema, but it should be pretty straightforward if you look at JPA examples and demos out there with OneToOne, ManyToOne and OneToMany relationships - they all use constraints. – Chris Jan 24 '22 at 20:23

1 Answers1

0

I managed to do this without adding useless tables or contort the ER logic.

As I said I have a MonitorPeriod:

@Entity
@Table(name = "monitor_period")
@Data
public class MonitorPeriod {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  private LocalDate dateFrom;
  private LocalDate dateTo;

  @OneToMany(mappedBy = "monitorPeriod")
  private List<Monitor> monitor;
}

Now I have my File:

@Entity
@Table(name = "file")
@Data
public class File {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  @OneToMany(mappedBy = "file")
  private List<Monitor> monitor;
}

As said before, File (in his life) can has more Monitor but just one per MonitorPeriod

So the Monitor table:

@Entity
@Table(name = "monitor", uniqueConstraints = { 
    @UniqueConstraint(name = "UniquePeriodoAndMonitor", columnNames = { "id_period", "id_file" }) 
    }
)
@Data
public class Monitor {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  private Boolean compilato;

  @ManyToOne(optional = false, fetch = FetchType.LAZY)
  @JoinColumn(name = "id_period")
  private MonitorPeriod monitorPeriod;

  @ManyToOne(optional = false, fetch = FetchType.LAZY)
  @JoinColumn(name = "id_file")
  private File file;
}

Hope helps..

CoderJammer
  • 599
  • 3
  • 8
  • 27