I have a doubt about how the modeling of my entity would be. Come on, I have a table in the database that serves to save documents from my system, this table has the columns id, fk_id (element foreign key), fk_table (entity name) and file_name (stores the name of my file) .
I did a lot of research before posting my question here, but I didn't find anything related to it, what would my entities, user, patient and doctor?
DB:
| id | fk_id | fk_table | file_name |
|---|---|---|---|
| 1 | 21 | user | test1.jpg |
| 2 | 32 | doctor | test2.pdf |
| 3 | 61 | user | test10.pdf |
| 4 | 100 | patient | test5.jpg |
Class:
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String LastName;
// What would a one-to-many relationship look like?
}
public class patient{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
// What would a one-to-many relationship look like?
}
CodePudding user response:
You can use @Where. But be aware that @Where is a Hibernate annotation. It's not in the JPA standard.
For example in the User entity: (I assume that your table is mapped to an entity called Document)
@Where( clause = "fk_table = 'user'")
@JoinColumn(name = "fk_id")
@OneToMany
private List<Document> documents = new ArrayList<>( );
CodePudding user response:
The following is based only on standard JPA annotations. The idea is to create an inheritance hierarchy for the documents table. The base is:
@Entity
@Table(name = "XX_DOCUMENT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "fk_table")
public abstract class BaseDocument {
@Id
@GeneratedValue(strategy=SEQUENCE)
private Long id;
@Column(name = "file_name")
private String fileName;
}
Here we define that all entities extending this will go to the same table, with the fk_table column to discriminate. The entities extending it are defined as follows:
@Entity
@DiscriminatorValue("doctor")
public class DoctorDocument extends BaseDocument {
@ManyToOne
@JoinColumn(name = "fk_id")
private Doctor doctor;
}
@Entity
@DiscriminatorValue("patient")
public class PatientDocument extends BaseDocument {
@ManyToOne
@JoinColumn(name = "fk_id")
private Patient patient;
}
// and so on
The interesting thing is that we are reusing the column fk_id to point to the right table. From a small experiment, Hibernate seems to not have problems with it. I would suggest that you manage the DB creation another way just to be safe.
The Doctor, Patient etc need not have a common base class, e.g.:
@Entity
@Table(name = "XX_DOCTOR")
public class Doctor {
@Id
@GeneratedValue(strategy=SEQUENCE)
private Long id;
@OneToMany(mappedBy = "doctor")
private Collection<DoctorDocument> documents = new ArrayList<>();
// any doctor-specific fields
}
@Entity
@Table(name = "XX_PATIENT")
public class Patient {
@Id
@GeneratedValue(strategy=SEQUENCE)
private Long id;
@OneToMany(mappedBy = "patient")
private Collection<PatientDocument> documents = new ArrayList<>();
// any patient-specific fields
}
// and so on
You can read a (doctor, patient, ...)'s documents from the relevant collection. You can even query BaseDocument instances based on any criteria.
You can even go ahead and do more fabcy stuff with the Java code. E.g. define an interface HasDocuments:
public interface HasDocuments<D extends BaseDocument> {
Collection<D> getDocuments();
}
Doctor, Patient, ..., implements this, so they can all be treated the same way.
