简体   繁体   English

来自实体的JPA实体管理器createQuery,不同的包但名称相同

[英]JPA entity manager createQuery from entity, different package but same name

I have the following entities in the module 我在模块中有以下实体

com.example.Company (table_name: COMPANIES) com.example.Company(表名称:COMPANIES)

com.example.workspace.Company (table_name: WS_COMPANIES) com.example.workspace.Company(表名称:WS_COMPANIES)

in main class, i only import the entity import com.example.Company . 在主类中,我仅导入实体import com.example.Company Then I want to find all companies from DB 然后,我想从数据库中找到所有公司

TypedQuery<Company> query = (javax.persistence.EntityManager) em.createQuery("from Company", Company.class);

however, i got the error where it tries to query 但是,我在尝试查询的地方出现了错误

Type specified for TypedQuery [com.example.Company] is incompatible with query return type [class com.example.workspace.Company] 为TypedQuery [com.example.Company]指定的类型与查询返回类型[class com.example.workspace.Company]不兼容

Is there any method to resolve this, and appreciate if anyone could explain how the entity manager construct the create query? 是否有任何方法可以解决此问题,并感谢有人可以解释实体管理器如何构造创建查询?

em.createQuery("from com.example.Company", Company.class) seems to be a way, but it doesn't looks elegant. em.createQuery("from com.example.Company", Company.class)似乎是一种方法,但看起来并不优雅。 I might want to have com.example.Company as the default 我可能想将com.example.Company作为默认值

the below is the persistence.xml file, the classes are in sequence 以下是persistence.xml文件,这些类按顺序排列

<persistence-unit name="pcc-cpod-persistence-unit" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <class>com.example.Company</class>
  <class>com.example.workspace.Company</class>
  <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

From the JPA specifications: 根据JPA规范:

4.3.1 Naming 4.3.1命名

Entities are designated in query strings by their entity names. 实体在查询字符串中由实体名称指定。 The entity name is defined by the name element of the Entity annotation (or the entity-name XML descriptor element), and defaults to the unqualified name of the entity class. 实体名称由Entity批注的name元素(或Entity-name XML描述符元素)定义,并且默认为实体类的非限定名称。 Entity names are scoped within the persistence unit and must be unique within the persistence unit. 实体名称的范围在持久性单元内,并且在持久性单元内必须唯一。

Based on this, "from Company" refers to the @Entity with a name="Company". 基于此,“来自公司”指代名称为“公司”的@Entity。 As you are not permitted to have duplicate names, it must be that com.example.Company has a different entity name than "Company". 由于不允许使用重复的名称,因此必须是com.example.Company与“ Company”具有不同的实体名称。 If you look at the @Entity definition for com.example.Company you should find its name, and if you use that name in the SELECT query you should get your desired result. 如果查看com.example.Company的@Entity定义,则应找到其名称,如果在SELECT查询中使用该名称,则应获得所需的结果。

You can use "name" attribute of "Entity" annotation to distinguish between entities that share same class name and you can refer the value specified in this name" attribute while building the JPQL query. 您可以使用“实体”注释的“名称”属性来区分共享相同类名的实体,并且可以在构建JPQL查询时引用此名称中指定的值。

In your case, you can have 就您而言,您可以

@Entity(name="DefaultCompany")
@Table(name="COMPANIES")
for com.example.Company

@Entity(name="WSCompany")
@Table(name="WS_COMPANIES")
for com.example.workspace.Company 

And in you queries you can refer the entities as below: 在查询中,您可以参考以下实体:

em.createQuery("from DefaultCompany", Company.class);
em.createQuery("from WSCompany", Company.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用来自Hibernate实体管理器的find和createQuery的不同SQL - Different SQL using find and createQuery from Hibernate entity manager Hibernate:为什么createQuery()将包名称附加到实体名称? - Hibernate : why createQuery() appends package name to the Entity Name? HIbernate两个实体在不同的程序包中具有相同的名称,但没有例外 - HIbernate two entity with same name in different package but no exception JPA Spring 将实体保存为 MariaDB 中的不同名称 - JPA Spring saving entity to a different name in MariaDB JPA 2 个不同的本地查询,具有相同的 class 作为实体 - JPA 2 different native queries with same class as entity 如何在JPA中的不同关系中使用同一实体? - How to use same entity in different relations in JPA? JPA,删除由其他经理找到的实体 - JPA, removing an entity which has found by different manager Java Hibernate JPA 创建一个实体并加入同一列引用的两个不同的表,具有相同的列名 - Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name 原因:org.hibernate.AnnotationException:在不同包中的实体上使用两次相同的实体名称,但是名称相同 - Caused by: org.hibernate.AnnotationException: Use of the same entity name twice on entities in different package but same name 为什么要使用JPA实体管理器? - JPA Entity Manager why to use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM