JPA:名称的命名查询:...未找到
JPA: Named Query of name: ... not found
我正在公司使用 JPA 开发多租户应用程序。因此,我使用 TABLE_PER_TENANT 功能将租户特定数据分离到不同的模式中。将数据推送到数据库没问题,工作正常。问题只是从数据库中读取数据,应用程序找不到我的命名查询。
我已经尝试过不同的东西:
- 重新编译和部署应用程序
- 移动 persistence.xml 到不同的文件夹
- 查看了错误日志并调试了整个应用程序,找不到原因
- 查看了我在 Google 上可以找到的所有可能的解决方案并尝试了它们...
我在这里错过了什么?有什么想法吗?
BaseObject.java
@MappedSuperclass
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty="tenant.id")
public abstract class BaseObject {
/**
* The (globally unique) ID of the object.
*/
@Id
@Column(name = "GUID", length = 36)
private String guid = UUID.randomUUID().toString();
/**
* The {@link Date} the object was created at.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATION_DATE", updatable = false)
private Date createdAt = null;
/**
* The {@link Date} the object was last modified at.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFICATION_DATE")
private Date lastModifiedAt = null;
/**
* ID of the user who created the object.
*/
@Column(name = "CREATED_BY", updatable = false, length = 20)
private String createdBy = null;
/**
* ID of the user who was the last to modify the object.
*/
@Column(name = "MODIFIED_BY", length = 20)
private String lastModifiedBy = null;
// Methods...
}
Plant.java
@Entity
@Table(name = "PLANT_POLLUTION_DATA")
@NamedQueries({ @NamedQuery(name = "Plant.getPlants", query = "SELECT c FROM Plant c"),
@NamedQuery(name = "Plant.getPlantById", query = "SELECT c FROM Plant c WHERE c.id = :id") })
@XmlRootElement(name = "plantlist")
@XmlAccessorType(XmlAccessType.FIELD)
public class Plant extends BaseObject implements Serializable {
/**
* The <code>serialVersionUID</code> of the {@link Plant} class.
*/
private static final long serialVersionUID = 1L;
@Column(name = "ID", length = 36, nullable = true)
String id = null;
@Column(name = "O3", length = 10, nullable = true)
String o3 = null;
@Column(name = "DATE_FIELD")
java.sql.Date dateField = null;
@Column(name = "location", length = 36, nullable = true)
String location = null;
// Methods...
}
NamedQuery 调用
String tenantId = getTenantId();
Map<String, String> props = new HashMap<String, String>();
props.put("tenant.id", tenantId);
EntityManager em = this.getEntityManagerFactory().createEntityManager(props);
Query query = em.createNamedQuery("Plant.getPlantById");
query.setParameter("id", id);
retVal = query.getResultList();
persistence.xml(位于"file.war/WEB-INF/classes/META-INF/persistence.xml")
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pollutionmonitoring" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.company.cloud.samples.pollutionmonitoring.model.BaseObject</class>
<class>com.company.cloud.samples.pollutionmonitoring.model.Plant</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
我可以通过添加以下两行代码自己解决这个问题:
...
EntityManager em = this.getEntityManagerFactory().createEntityManager(props);
em.getTransaction().begin();
em.getTransaction().commit();
Query query = em.createNamedQuery("Plant.getPlantById");
...
我不知道这个改变到底做了什么,但它对我有用:)也许有人知道为什么这个解决方案有效?
PS:我只是从我的另一个 class 复制它,我必须在其中将数据添加到数据库,这是两个 class 之间的唯一区别。
我正在公司使用 JPA 开发多租户应用程序。因此,我使用 TABLE_PER_TENANT 功能将租户特定数据分离到不同的模式中。将数据推送到数据库没问题,工作正常。问题只是从数据库中读取数据,应用程序找不到我的命名查询。
我已经尝试过不同的东西:
- 重新编译和部署应用程序
- 移动 persistence.xml 到不同的文件夹
- 查看了错误日志并调试了整个应用程序,找不到原因
- 查看了我在 Google 上可以找到的所有可能的解决方案并尝试了它们...
我在这里错过了什么?有什么想法吗?
BaseObject.java
@MappedSuperclass
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty="tenant.id")
public abstract class BaseObject {
/**
* The (globally unique) ID of the object.
*/
@Id
@Column(name = "GUID", length = 36)
private String guid = UUID.randomUUID().toString();
/**
* The {@link Date} the object was created at.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATION_DATE", updatable = false)
private Date createdAt = null;
/**
* The {@link Date} the object was last modified at.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFICATION_DATE")
private Date lastModifiedAt = null;
/**
* ID of the user who created the object.
*/
@Column(name = "CREATED_BY", updatable = false, length = 20)
private String createdBy = null;
/**
* ID of the user who was the last to modify the object.
*/
@Column(name = "MODIFIED_BY", length = 20)
private String lastModifiedBy = null;
// Methods...
}
Plant.java
@Entity
@Table(name = "PLANT_POLLUTION_DATA")
@NamedQueries({ @NamedQuery(name = "Plant.getPlants", query = "SELECT c FROM Plant c"),
@NamedQuery(name = "Plant.getPlantById", query = "SELECT c FROM Plant c WHERE c.id = :id") })
@XmlRootElement(name = "plantlist")
@XmlAccessorType(XmlAccessType.FIELD)
public class Plant extends BaseObject implements Serializable {
/**
* The <code>serialVersionUID</code> of the {@link Plant} class.
*/
private static final long serialVersionUID = 1L;
@Column(name = "ID", length = 36, nullable = true)
String id = null;
@Column(name = "O3", length = 10, nullable = true)
String o3 = null;
@Column(name = "DATE_FIELD")
java.sql.Date dateField = null;
@Column(name = "location", length = 36, nullable = true)
String location = null;
// Methods...
}
NamedQuery 调用
String tenantId = getTenantId();
Map<String, String> props = new HashMap<String, String>();
props.put("tenant.id", tenantId);
EntityManager em = this.getEntityManagerFactory().createEntityManager(props);
Query query = em.createNamedQuery("Plant.getPlantById");
query.setParameter("id", id);
retVal = query.getResultList();
persistence.xml(位于"file.war/WEB-INF/classes/META-INF/persistence.xml")
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pollutionmonitoring" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.company.cloud.samples.pollutionmonitoring.model.BaseObject</class>
<class>com.company.cloud.samples.pollutionmonitoring.model.Plant</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
我可以通过添加以下两行代码自己解决这个问题:
...
EntityManager em = this.getEntityManagerFactory().createEntityManager(props);
em.getTransaction().begin();
em.getTransaction().commit();
Query query = em.createNamedQuery("Plant.getPlantById");
...
我不知道这个改变到底做了什么,但它对我有用:)也许有人知道为什么这个解决方案有效?
PS:我只是从我的另一个 class 复制它,我必须在其中将数据添加到数据库,这是两个 class 之间的唯一区别。