I have 2 java classes, Relation
and Person
, which both are present in my database.
Person:
@Entity
@Table(name = "persons")
public class Person {
@Id
@Column
private int id;
@Column
private String name;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "slave_id", referencedColumnName="id"),
@JoinColumn(name = "master_id", referencedColumnName="id")
})
private List<Relation> relations;
//Getters and setters
}
Relation:
@Entity
@Table(name = "relations")
public class Relation {
@Id
@Column
private int id;
@Column
private int child_id;
@Column
private int parent_id;
@Column
private String type;
//Getters and setters
}
Each Person has a list of relations (or not), the relation should be added to the list when the child_id or the parent_id of the relation is equal to the id of the person.
TL;DR:
When relation.child_id OR relation.parent_id = person.id
=> add relation to list of relations to the person
The issue I am facing is that this annotation:
@JoinColumns({
@JoinColumn(name = "child_id", referencedColumnName="id"),
@JoinColumn(name = "parent_id", referencedColumnName="id")
})
creates following SQL (just the necessary part):
relations relations6_
on this_.id=relations6_.slave_id
and this_.id=relations6_.master_id
What is the correct annotation in Java Hibernate to generate an SQL statement saying OR instead of AND
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…