There are three entity entities representing classes, teachers and students like this:
{code:java} @Entity @Table(indexes = {}) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "non-lazy") @SelectBeforeUpdate(true) @DynamicUpdate @DynamicInsert public class EStudent {
@Id @NotNull @Size(min = 36, max = 36) @Column(name = "id_") String id;
@Column String name;
@ManyToOne(cascade = { javax.persistence.CascadeType.REFRESH }, optional = false, fetch = FetchType.LAZY) EClass clazz;
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, mappedBy = "students") List<ETeacher> teachers; } {code}
{code:java} @Entity @Table(indexes = {}) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "non-lazy") @SelectBeforeUpdate(true) @DynamicUpdate @DynamicInsert public class EClass {
@Id @NotNull @Size(min = 36, max = 36) @Column(name = "id_") String id;
@Column String name;
@OneToMany(cascade = javax.persistence.CascadeType.REFRESH, fetch = FetchType.LAZY,mappedBy = "clazz") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "non-lazy") List<EStudent> students;
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, mappedBy = "classes") List<ETeacher> teachers; } {code}
{code:java} @Entity @Table(indexes = {}) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "non-lazy") @SelectBeforeUpdate(true) @DynamicUpdate @DynamicInsert public class ETeacher{
@Id @NotNull @Size(min = 36, max = 36) @Column(name = "id_") String id;
@Column String name;
@ManyToMany(cascade = javax.persistence.CascadeType.REFRESH, fetch = FetchType.LAZY) @JoinTable(joinColumns = @JoinColumn(name = "teacher_id_"), inverseJoinColumns = @JoinColumn(name = "class_id_")) List<EClass> classes;
@ManyToMany(cascade = javax.persistence.CascadeType.REFRESH, fetch = FetchType.LAZY) @JoinTable(joinColumns = @JoinColumn(name = "teacher_id_"), inverseJoinColumns = @JoinColumn(name = "student_id_")) List<EStudent> students; } {code}
The JPQL: {code:sql} from com.yzt.microservice.core.ips.schema.demo.EStudent as eStudent inner join eStudent.clazz.teachers as eTeacher on eTeacher.name = 'Alice' {code}
This JPQL execute failure cause by "Unknown column 'eteacher3_.name_' in 'on clause'". The generate sql: {code:sql} SELECT estudent0_.id_ AS id_1_41_0_, eteacher3_.id_ AS id_1_72_1_, estudent0_.clazz_id_ AS clazz_id3_41_0_, estudent0_.name_ AS name_2_41_0_, eteacher3_.name_ AS name_2_72_1_ FROM estudent_ estudent0_ INNER JOIN eclass_ eclass1_ ON estudent0_.clazz_id_=eclass1_.id_ AND ( eteacher3_.name_='Alice') INNER JOIN eteacher_eclass_ teachers2_ ON eclass1_.id_=teachers2_.class_id_ INNER JOIN eteacher_ eteacher3_ ON teachers2_.teacher_id_=eteacher3_.id_ {code}
|
|