Ok, i've been searching and experimenting on how to do this, and i have found zero examples or documentation on this subject... Here is the problem:
I have an abstract class like this (simplified):
Code:
@MappedSuperclass
public abstract class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Basic
private String name;
@Basic
private String telephone;
//more properties, like: address, email, etc...
}
Then i have 3 childs, that looks like this:
Code:
@Entity
@Table(name="TBL_USERS")
public class User extends Person implements Serializable {
@Basic
private String username;
@Basic
private String password;
//more user-specific fields
}
Code:
@Entity
@Table(name="TBL_CONTACTS")
public class Contact extends Person implements Serializable {
@Basic
private String company;
//more contact-specific fields
}
Code:
@Entity
@Table(name="TBL_CLIENTS")
public class Client extends Person implements Serializable {
@ManyToOne
@JoinColumn(name="asesor_fk")
private User asesor;
//more client-specific fields
}
Ok, everything here is perfect, the Application works fine, with no problems at all.
Ok, the problem is that i want to have something like this:
Code:
@Entity
@Table(name="TBL_MEETINGS")
public class Meeting implements Serializable {
//meeting-specific fields..., like id, date, time, place, etc...
@ManyToMany
@JoinTable(name = "TBL_MEETING_GUESTS",
joinColumns = @JoinColumn(name = "meeting_fk"),
inverseJoinColumns = @JoinColumn(name = "person_fk"))
private List<Person> guests; //THIS IS THE PROBLEM!!
}
The problem is that i don't know how to specify a database column wich would contain a discriminator value inside TBL_MEETING_GUESTS, this way, Hibernate could read this column, and load the record from the corresponding table. For example, to have something like this:
Code:
@ManyToMany
@JoinTable(name = "TBL_MEETING_GUESTS",
joinColumns = @JoinColumn(name = "meeting_fk"),
inverseDiscriminatorColumn = @DiscriminatorColumn(name = "person_type", discriminatorType = DiscriminatorType.STRING),
inverseJoinColumns = @JoinColumn(name = "person_fk"))
private List<Person> guests;
And then add a discriminator value to the classes: User, Contact and Client. This discriminator value should be used only on the Meeting class.
Well, this is what i want to do. Reading around the web, i saw something about writing a custom EntityPersister. Maybe this can be the solution, but i only need the custom persister on the Meeting class.
I want to know if this is the way to go, or if there is an easier way to implement this.
Thanks :)