I create 2 entity classes like that
@Entity
@Table(name = "clazz_inf")
public class Clazz
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "clazz_code")
private Integer code;
private String name;
@ManyToMany(fetch = FetchType.LAZY, targetEntity = Student.class)
@JoinTable(
name = "student_clazz",
joinColumns = @JoinColumn(name = "clazz_code"),
inverseJoinColumns = @JoinColumn(name = "student_id")
)
@WhereJoinTable(clause = "active = true")
private Set<Student> students = new HashSet<>();
...
@Entity
@Table(name = "student_inf")
public class Student
{
@Id
@Column(name = "student_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String address;
private int age;
private char gender;
@ManyToMany(fetch = FetchType.LAZY, targetEntity = Clazz.class)
@JoinTable(
name = "student_clazz",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "clazz_code")
)
private Set<Clazz> clazzes = new HashSet<>();
and I used the query like this
public void test() {
long count = (long) entityManagerFactory.createEntityManager().createQuery("SELECT count(std) FROM Clazz c join c.students std where c.id = :id")
.setParameter("id", 4)
.getSingleResult();
Assertions.assertEquals(1, count);
}
}
and I have the data like this:
insert into clazz_inf
values
(null, '疯狂Java训练营'),
(null, '疯狂Java就业班'),
(null, '疯狂Java基础班'),
(null, '疯狂Java提高班');
insert into student_inf
values
(null, 'aaa', 500, 'aaaaaa', 'male'),
(null, 'bbb', 800, 'bbbbbb', 'male'),
(null, 'ccc', 600, 'cccccc', 'male'),
(null, 'ddd', 580, 'dddddd', 'male'),
(null, 'eee', 23, 'eeeeee', 'female'),
(null, 'fff', 18, 'ffffff', 'female'),
(null, 'ggg', 21, 'gggggg', 'female'),
(null, 'hhh', 19, 'hhhhhh', 'female');
create table student_clazz
(
student_id int,
clazz_code int,
active boolean,
foreign key(clazz_code) references clazz_inf(clazz_code),
foreign key(student_id) references student_inf(student_id)
);
insert into student_clazz
values
(1, 1, true),
(1, 2, true),
(1, 3, false),
(1, 4, true),
(2, 1, true),
(2, 2, true),
(2, 4, false),
(3, 1, true),
(3, 3, true),
(3, 4, false),
(4, 1, true),
(4, 2, true),
(4, 4, false);
so if I use hibernate 6.3, then the unit test will pass, becaue hibernate will generate sql like this
select count(s1_0.student_id) from clazz_inf c1_0 join student_clazz s1_0 on c1_0.clazz_code=s1_0.clazz_code and (s1_0.active = 1) where c1_0.clazz_code=?
it means the @WhereJoinTable worked
but if I use hibernate 6.4, then the unit test will fail, becaue hibernate will generate sql like this
select count(s1_0.student_id) from clazz_inf c1_0 join student_clazz s1_0 on c1_0.clazz_code=s1_0.clazz_code where c1_0.clazz_code=?
it seems like that @WhereJoinTable annotation was ignored.
and I knew @WhereJoinTable annotation has been Deprecated, so I tried with @SQLJoinTableRestriction annotation.
I used @SQLJoinTableRestriction("active = true")
to replace @WhereJoinTable(clause = "active = true")
,
and I got the same result, it seems like that @SQLJoinTableRestriction annotation was ignored too
may I know what should I change to migrate to Hibernate 6.4