gpt4 book ai didi

java - 使用外键对 2 个表进行关联时出现 Criteria 问题

转载 作者:行者123 更新时间:2023-12-01 11:19:44 25 4
gpt4 key购买 nike

我正在尝试了解 hibernate 和 Criteria。我有两张 table ,分别是老鼠和疾病。我在 RATS 中设置了一个外键:RATS.Sickness_Id = SICKNESS.ID。

我正在尝试使用 Criteria 获得相当于 SQL 的功能:

select * from RATS r, SICKNESS s where s.id = r.sickness_id

我以为是这个协会:

session
.createCriteria(Rats.class)
.createCriteria(Sickness.class)
.toList()

不幸的是,这最终导致:org.hibernate.QueryException:无法解析属性:实体:entities.Rats

奇怪的是,两者:

session.createCriteria(Rats.class).toList()

    session.createCriteria(Sickness.class).toList()

work fine....

I'm a bit puzzled.
Here are my entities classes code:

@Entity
@Table(name = "RATS")
public class Rats implements java.io.Serializable {

private int id;
private Sickness sickness;
private String name;
private int age;

public Rats() {
}

public Rats(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

public Rats(int id, Sickness sickness, String name, int age) {
this.id = id;
this.sickness = sickness;
this.name = name;
this.age = age;
}

@Id
@Column(name = "ID", unique = true, nullable = false)
public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Sickness_Id")
public Sickness getSickness() {
return this.sickness;
}

public void setSickness(Sickness sickness) {
this.sickness = sickness;
}

@Column(name = "Name", nullable = false, length = 50)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "Age", nullable = false)
public int getAge() {
return this.age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
String returnString = "My name is " + getName() + ", I am " + getAge()+ ". ";
returnString += getSickness() == null ? "I am healthy like hell! :)" : "I suffer from " + getSickness().getNom();
return returnString;
}




}

@Entity
@Table(name = "SICKNESS")
public class Sickness implements java.io.Serializable {

private int id;
private String nom;
private Set<Rats> ratses = new HashSet<Rats>(0);

public Sickness() {
}

public Sickness(int id) {
this.id = id;
}

public Sickness(int id, String nom, Set<Rats> ratses) {
this.id = id;
this.nom = nom;
this.ratses = ratses;
}

@Id
@Column(name = "Id", unique = true, nullable = false)
public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

@Column(name = "Nom", length = 50)
public String getNom() {
return this.nom;
}

public void setNom(String nom) {
this.nom = nom;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sickness")
public Set<Rats> getRatses() {
return this.ratses;
}

public void setRatses(Set<Rats> ratses) {
this.ratses = ratses;
}

@Override
public String toString() {
return getNom()
+ ( getRatses() != null ? (", getRatses()=" + getRatses() + "]"): "" );
}



}

我错过了什么?提前致谢。

最佳答案

在 Rats 实体上,Sickness 实体属性为:

 private Sickness sickness;

因此,您的协会必须使用相同的名称。

session.createCriteria(Rats.class)
.createCriteria("sickness")
.list();

另一种解决方案,应该是将 Rats 更改为使用 EAGER Fetch:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Sickness_Id")
public Sickness getSickness() {
return this.sickness;
}

public void setSickness(Sickness sickness) {
this.sickness = sickness;
}

然后使用:

session.createCriteria(Rats.class)
.list();

关于java - 使用外键对 2 个表进行关联时出现 Criteria 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391927/

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