jpa本机查询检索多个实体

jpa本机查询检索多个实体

本文介绍了jpa本机查询检索多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个表的数据库:

I have a database with 4 tables:

company,staff,department,project

Company.java

@Entity
@Table(name = "company")
@SqlResultSetMapping(name = "COMPANY", entities =
{
    @EntityResult(entityClass = Company.class),
    @EntityResult(entityClass = Staff.class)
})
...

GetEntity.java

EntityManagerFactory emf = Persistence.createEntityManagerFactory("GetEntityPU");
EntityManager em = emf.createEntityManager();

String query = "SELECT *
                FROM company c
                JOIN staff s
                ON c.ID = s.companyID";
Query q = em.createNativeQuery(query, "COMPANY");
List<Object[]> list = q.getResultList();

从上面的代码中,我可以从Company实体和Staff实体检索所有数据.

From above code, I can retrieve all data from Company entity and Staff entity.

现在,我想从任何2个表中检索所有数据:
可能是公司,员工表的所有数据员工,部门表的所有数据

Now I want to retrieve all data from any 2 tables:
maybe all data for company, staff tables OR all data for staff, department tables

我应如何控制查询中的每个实体?
我真的不知道该怎么做.
欢迎任何想法或有用的源链接.

How should I control every entity in my query?
I really no ideas on how to do it.
Any ideas or useful source link are welcome.

推荐答案

将查询映射到bean可以为您提供帮助,请查看以下内容:查询bean

Mapping query to a bean could help you, check this out: query to bean

这篇关于jpa本机查询检索多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:40