基于子表的一对多分离 Criteria 父表获取
我在使用休眠进行一对多映射时遇到一个问题。
我有 2 个课程,人员和地址。人员按地址映射(一对多) 我想要获取所有地址=“xxxx”的人;。如何使用 DetachedCriteria 准备此查询。下面我添加了我的 dao 类中的一段代码。请帮我完成它。
Person.java
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;
@Column(name="personName")
private String name;
@OneToMany(cascade =CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name="personId")
private Set <Address> addresses;
}
Address.java
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
@Column(name = "address",nullable=false)
private String address;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="personId",nullable=false)
private Person person;
}
我的 DAO
DetachedCriteria c = DetachedCriteria.forClass(Person.class);
List<Person> persnList =null;
/*here i want add some restriction for
fetch all person whose address = "abcd"
here address is collection. how to set restriction in it ?.
*/
persnList = getHibernateTemplate().findByCriteria(c);
System.out.println(persnList.size());
select * from person where Address.address = "xxxx"; 如何使用 DetachedCriteria 实现此目的?
i have one problem in one-to-many mapping using hibernate.
i have 2 classes, Person and Address. Person is mapped by Address ( one-to-many)
i want get all Person where Address = "xxxx";. how to prepare this query using DetachedCriteria . below i have added a piece of code from my dao class. please help me to complete it.
Person.java
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;
@Column(name="personName")
private String name;
@OneToMany(cascade =CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name="personId")
private Set <Address> addresses;
}
Address.java
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
@Column(name = "address",nullable=false)
private String address;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="personId",nullable=false)
private Person person;
}
My DAO
DetachedCriteria c = DetachedCriteria.forClass(Person.class);
List<Person> persnList =null;
/*here i want add some restriction for
fetch all person whose address = "abcd"
here address is collection. how to set restriction in it ?.
*/
persnList = getHibernateTemplate().findByCriteria(c);
System.out.println(persnList.size());
select * from person where Address.address = "xxxx"; how to implement this using DetachedCriteria ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在主要标准上创建内部标准,这将执行等效的内部联接。
Creating inner criteria on main criteria which will do an equivalent inner join.