当前位置: 首页 > 知识库问答 >
问题:

将sql查询转换为CriteriaQuery

程胤运
2023-03-14

我有两个表,它们通过一个外键来维护它们之间的父子关系。查询如下所示。我想在使用jpa的同时使用标准版。所以有人可以帮助我使用标准版吗

Select parent.notification_id,parent.city,parent.name,parent.accountNo,
case when child.accountNo is not null then 'Yes' else 'No' end as checked
FROM parent
JOIN child ON parent.notification_id=child.notification_id_child
AND child.accountNo='test' WHERE parent.city='delhi' or parent.city='all' or parent.accountNo="test";

表“child”的“notification\u id\u child”列是外键,并引用表“parent”的主键。

共有2个答案

郁吉星
2023-03-14

最后,我设法解决了这个问题。我的实体类和条件查询如下所示。

父实体

@Entity
@Table(name="parent")
public class Parent{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name="notification_id")
   private Long notificationId;

   @Column(name="city")
   private String city;
   @Column(name="name")
   private String name;
   @Column(name="accountNo")
   private String accountNo;

   @JoinColumn(name="notification_id_child")
   @OneToMany
   private List<Child> child;

   //Getters Setters
}

子实体

@Entity
@Table(name="child")
public class Child{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name="id")
   private Long id;
   @Column(name="accountNo")   
   private String accountNo;
   @Column(name="notification_id_child")
   private String notificationIdChild;
   //Getters Setters
}

定义实体

public class CustomEntity{
    private Long notificationId;
    private String city;
    private String accountNo;
    private String checked;
}

条件查询

 @PersistenceContext
 EntitiManager em;    
 CriteraBuilder cb = em.getCriteriaBuilder();
 CriteriaQuery<CustomEntity> cq = cb.createQuery(CustomEntity.class);
 Root<Parent> parentEntity = cq.from(Parent.class);
 Join<Parent,Child> join = parentEntity.join("child", JoinType.LEFT);
 join.on(cb.equal(join.get("accountNo"),"test"));
 
 Path<String> notificationIdPath = parentEntity.get("notificationId");
 Path<String> cityPath = parentEntity.get("city");
 Path<String> accountNoPath = parentEntity.get("accountNo");
 cq.multiselect(notificationIdPath, cityPath, accountNoPath,
 cb.selectCase().when(join.get("accountNo").isNotNull(),"Yes").otherwise("No"));
 
 Path<String> accountNoPath = parentEntity("accountNo");
 Predicate accountNoPredicate = cb.equal(accountNoPath, "test"); 
 Predicate cityPredicateAll = cb.equal(cityPath,"all");
 Predicate cityPredicateSpecified = cb.equal(cityPath,"delhi");
 
 cq.where(cb.or(cityPredicateAll, cityPredicateSpecified, accountNoPredicate));

 TypedQuery<CustomEntity> query = em.createQuery(cq);
 List<CustomEntity> CustomEntityList = query.getResult();
金晨
2023-03-14

您可以使用多种策略来实现这一点:

  1. 映射超类(父类将使用此注释而不是实体映射)
  2. 单表(每个层次结构的单表,可以使用@判别列JPA注释来识别每个层次结构)
  3. 连接表(父类和子类的每个类)在这种情况下,您必须连接公共列上的两个表才能获取结果。

以下是关于连接表的一些很好的答案

在Spring Data JPA中连接两个表实体

有关discrimintaorColumn用法的一些好答案的链接

如何访问JPA中的鉴别器列

 类似资料:
  • 问题内容: 我想将以下SQL查询转换为Elasticsearch之一。谁能帮上忙 我尝试了以下方法: 但不确定我是否做对了,因为它无法验证结果。似乎要在聚合内添加查询。 问题答案: 假设您使用Elasticsearch 2.x,则有可能在Elasticsearch中 具有 -semantics。我不知道2.0之前的可能性。 您可以使用新的Pipeline Aggregation Bucket Se

  • 问题内容: 多亏了Erwin Brandstetter在我之前的问题“具有has_many关系的订单”中的帮助,我的SQL查询才能正常工作。 如何将该SQL转换为ActiveRecords或AREL查询以在范围中使用? 我最近来的是在朋友的帮助下… …这给了我一个错误: 更新: 我之前的问题对相关的架构和查询有完整的描述。但是基本上Articles have_many Metrics和一个Metr

  • 问题内容: 我在SQL Server 2008中具有下表: 我需要创建以下输出的查询: 列可以固定为[Mitarbeiter1]-[Mitarbeiter5],因为每个Filiale的行数不能超过5行。 非常感谢您的帮助! 问题答案: 使用SQL Server 2008,将Pivot和Rank函数组合在一起,可以为每个雇员数量提供理想的结果。首先,我们为每个分支中的每个员工分配一个ID,从每个新分

  • 问题内容: 我有一个如下所示的表。我的问题是:如何将列转换为行?我正在使用Microsoft SQL Server 我需要像下面的操作 怎么做?谢谢 问题答案: 您的资料 询问 结果集

  • 在我们的应用程序中,数据库将从SQL Server更改为DB2。

  • 我是Django ORM的新手,正在努力将以下查询转换为ORM。感谢任何帮助/指导。日期/时间不按特定顺序排列。 提前谢谢 SQL查询:<br>从<br>中选择<br>A.Person_id、<br>A.Dept_id、<br>A.score_date、<br>A.score<br>作为<br>内部联接<br>(从借款者deptscore分组中选择Person_ id、Dept_ id、MAX(s