使用外键在 2 个表上建立关联时标准问题
issue with Criteria when making an association on 2 tables with a fereign key
我正在尝试了解休眠和标准。
我有 2 张桌子 RATS 和 SICKNESS。
我在 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();
我正在尝试了解休眠和标准。 我有 2 张桌子 RATS 和 SICKNESS。 我在 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();