I’m having a small and weird issue using Hibernate. I have two tables: “SERVICE” & SERVICE_RELATIONSHIP that defines a relationship between two services.
CREATE TABLE TMS.TB_SERVICE (
ID_SERVICE INTEGER NOT NULL,
NAME VARCHAR(255),
)
CREATE TABLE SERVICE_RELATIONSHIP (
ID_SERVICE INTEGER NOT NULL,
ID_SERVICE_REL INTEGER NOT NULL,
RELATIONSHIP VARCHAR(1) // Could be 'E' (Exclude) or 'I' (Include)
)
I use different methods (getInclude and getExclude) in order to get services depending on the type of relationship that they have. They work perfect, the only problem is when I want to persist a service, ID_SERVICE & ID_SERVICE_REL columns are inserted correctly but not RELATIONSHIP.
@Table(name="SERVICE")
public class Service implements Serializable {
private String name;
private Integer id;
private Collection<Service> exclude;
private Collection<Service> include;
[..]
@JoinTable(name = "SERVICE_RELATIONSHIP",
joinColumns = {
@JoinColumn(name="ID_SERVICE", unique = false, updatable = true)
},
inverseJoinColumns = {
@JoinColumn(name="ID_SERVICE_REL")
}
)
@WhereJoinTable(clause = "RELATIONSHIP='E'")
public Collection<Service> getExclude() {
return exclude;
}
[..]
@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name = "SERVICE_RELATIONSHIP",
joinColumns = {
@JoinColumn(name="ID_SERVICE", unique = false, updatable = true)
},
inverseJoinColumns = {
@JoinColumn(name="ID_SERVICE_REL")
}
)
@WhereJoinTable(clause = "RELATIONSHIP='I'")
public Collection<Service> getInclude() {
return include;
}
}
Any ideas?
Thanks in advance
@WhereJoinTableas the name describes, is only used for Where condition when doing a join on a select clause.You have a couple of options to implement this.
Personally, I would map the relationship class, but I would keep it hidden from outside
Service, so the code that uses service is not aware of the relationship class.