gpt4 book ai didi

java - @OneToMany 关联的标准

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:10 24 4
gpt4 key购买 nike

Student 类与 Resume 类型的对象具有 OneToOne 关联,Resume 类与 Master 类型的集合具有 OneToMany 关联,并且 master 具有属性 degreeName。我想要实现的是:

选择student.resume.masters.degreeName在(?,?,?,.....)中的所有学生

它应该搜索所有集合(主)对象。

下面的代码没有编译错误,但没有给出预期的结果,请指正。

学生实体:

 @Entity
public class Student {
@OneToOne(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL)
private Resume resume;
}

简历实体:

@Entity
public class Resume {
@OneToMany(mappedBy="resume",cascade=CascadeType.ALL)
private List<Master> masters=new ArrayList<>();
@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private Student student;
}

主实体:

@Entity
public class Master {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="resume_id")
private Resume resume;
private String degreeName;
}

道:

    Criteria studentCriteria = session.createCriteria(Student.class);
Criteria resumeCriteria = studentCriteria.createCriteria("resume");
Criteria mastersCriteria = resumeCriteria.createCriteria("masters");
List<String> degreeslist = new ArrayList<>(Arrays.asList(degrees));
//degreeList is collection of values on which the student will be searched

if (degreeslist.size() == 1) {
mastersCriteria.add(Restrictions.eq("degreeName",
degreeslist.get(0)));
} else {

mastersCriteria.add(Restrictions.in("degreeName", degreeslist));
}

最佳答案

    Criteria mastersCriteria = getSession().createCriteria(Student.class, "student");
mastersCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
mastersCriteria.createAlias("student.resume", "resume");
mastersCriteria.createAlias("resume.masters","masters");

if (degreeslist.size() == 1) {
mastersCriteria.add(Restrictions.eq("masters.degreeName", degreeslist.get(0)));
} else {
mastersCriteria.add(Restrictions.in("masters.degreeName", degreeslist));
}


List<Student> students = mastersCriteria.list();

关于java - @OneToMany 关联的标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com