I have 3 tables, but I'm only interested in
- table A
- the 'amount' field from A_Cat (this is the join table between "A" and "Category") for a well defined value of cat_Id. This predicate makes the relation a OneToOne instead of a OneToMany.
This is the simplified structure:
CREATE TABLE A
(
a_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL
);
CREATE TABLE Category
(
cat_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL
);
CREATE TABLE A_Category
(
a_id INT PRIMARY KEY,
cat_id INT PRIMARY KEY,
amount INTEGER NOT NULL
);
Java code:
@Entity
@Table(name = "A")
@SecondaryTable(name = "A_Category", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "a_id")}))
public class ModelA {
@Id
@Column(name = "a_id")
private Long id;
@Column(name = "amount", table = "A_Category")
private Integer amount;
}
This generates a join in Hibernate between "A" and "A_Category" for all the possible values.
So, here's my question, how to add a predicate to the SecondaryTable (eg: cat_id = 1).
Can it be done with an annotation? I tried it with @WhereJoinTable but no luck.
PS: I can obtain what I want if I use @Formula, but if I want more fields from A_Category
then the generated sql is not that performant and like to avoid writing sql in my POJO.