基于子表的一对多分离 Criteria 父表获取

发布于 2024-12-11 13:53:31 字数 1571 浏览 0 评论 0原文

我在使用休眠进行一对多映射时遇到一个问题。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-12-18 13:53:31
DetachedCriteria c = DetachedCriteria.forClass(Person.class);
    List<Person> persnList =null;

DetachedCriteria addrCrit = c.createCriteria("addresses").addRestriction(Restrictions.eq("address","abcd"));
addrCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    persnList = getHibernateTemplate().findByCriteria(c);
    System.out.println(persnList.size());

在主要标准上创建内部标准,这将执行等效的内部联接。

DetachedCriteria c = DetachedCriteria.forClass(Person.class);
    List<Person> persnList =null;

DetachedCriteria addrCrit = c.createCriteria("addresses").addRestriction(Restrictions.eq("address","abcd"));
addrCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    persnList = getHibernateTemplate().findByCriteria(c);
    System.out.println(persnList.size());

Creating inner criteria on main criteria which will do an equivalent inner join.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文