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
Annotation Type JoinColumn. Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply. (Optional) The SQL fragment that is used when generating the DDL for the column.
Annotation Type JoinTable. Specifies the mapping of associations. It is applied to the owning side of an association. A join table is typically used in the mapping of many-to-many and unidirectional one-to-many associations.
The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.
The purpose of the MappedBy parameter is to instruct JPA: Do NOT create another join table as the relationship is already being mapped by the opposite entity of this relationship.
Some of the options that you could utilize:
Relation
entity.@Subselect
. This is a kind of Hibernate view, suitable if you can't afford to create a real database view or change the db schema to better suit the entity model structure.This and this answer could also be helpful.
Also, you can always use two separate associations for slaves and masters:
public class Person {
@OneToMany
@JoinColumn(name = "slave_id"),
private List<Relation> slaves;
@OneToMany
@JoinColumn(name = "master_id"),
private List<Relation> masters;
public List<Relation> getRelations() {
List<Relation> result = new ArrayList<>(slaves);
result.addAll(masters);
return result;
}
}
However, keep in mind that joining all of them in a single query requires full Cartesian product between masters and slaves.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With