休眠标准,以从一对多关系中提取记录 [英] Hibernate Criteria to pull records from OneToMany Relations

查看:51
本文介绍了休眠标准,以从一对多关系中提取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在传递一组车辆名称,例如 ['car','jeep','truck','bike'] ,并希望选择拥有车辆的那些车主在使用Criteria查询的此列表中,此处的所有者可以拥有多个 vahicles (OneToMany).我有一个限制,我需要使用条件查询.

I am passing a collection of vehicles names like ['car','jeep','truck','bike'] and want to select those owners who owns vehicles in this list using Criteria query, Owner here can own multiple vahicles (OneToMany). I have a limitation that i need to use Criteria query.

class Owner {

    @ID
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "owner_id")
    private Long ownerId;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "owner_id")
    private Set<Vehicles> vehicles;

}

class Vehicles {

    @ID
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "vehicle_id")
    private Long vehicleId;

    @ManyToOne
    @JoinColumn(name = "owner_id")
    private Owner owner;

    @Column(name="vehicle_name")
    private String vehicleName;

}

下面是我尝试过但没有成功

Below is what i have tried but no success

Criteria criteria = getSession().createCriteria(Owner.class);
criteria.createAlias("vehicles", "vehicles");
criteria.add(Restrictions.in("vehicles.vehicleName", setOfVehicles));
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

推荐答案

首先,您的映射看起来不正确.您在 Owner Vehicles 之间建立了双向关联.因此,只有 @ManyToOne 一侧应由 @JoinColumn 进行注释.我将通过以下方式使用更正的映射:

First of all, your mapping looks incorrect. You have a bidirectional association between Owner and Vehicles. So, only the @ManyToOne side should be annotated by @JoinColumn. I will use the corrected in the following way mapping:

@Entity
@Table
public class Owner
{
   @Id
   @Column(name = "own_id")
   private Long id;

   @OneToMany(mappedBy = "owner")
   private Set<Vehicles> vehicles;
}

@Entity
@Table
public class Vehicles
{
   @Id
   @Column(name = "veh_id")
   private Long id;

   @Column(name = "veh_name")
   private String name;

   @ManyToOne
   @JoinColumn(name = "veh_own_id")
   private Owner owner;
}

接下来要强调的是,您尝试使用已弃用的Hibernate org.hibernate.Criteria 标准API .因此,查询将具有以下视图:

Next thing that I have to emphasize, you try to use deprecated Hibernate org.hibernate.Criteria API. I am going to provide an example for the Criteria API. So, the query will have the following view:

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Owner> criteria = builder.createQuery(Owner.class);
Root<Owner> root = criteria.from(Owner.class);
SetJoin<Owner, Vehicles> vehs = (SetJoin<Owner, Vehicles>) root.fetch(Owner_.vehicles);

criteria
   .select(root)
   .distinct(true)
   .where(
      vehs.get(Vehicles_.NAME).in(Arrays.asList("car", "jeep", "truck", "bike"))
   );

List<Owner> deps = session.createQuery(criteria).getResultList();

Owner _ Vehicles _ 类属于所谓的

The Owner_ and Vehicles_ classes belong to so called JPA static metamodel. Why the casting (SetJoin<Owner, Vehicles>) root.fetch(Owner_.vehicles) is necessary described in this article (see Defining a JOIN FETCH clause section).

这篇关于休眠标准,以从一对多关系中提取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆