我正在使用JPA在我的公司開發多租戶應用程序。所以我使用TABLE_PER_TENANT功能將租戶特定數據分成不同的模式。將數據推入數據庫是沒有問題的,可以正常工作。問題只是從數據庫中讀取數據,應用程序找不到我的命名查詢。JPA:名稱的查詢名稱:...未找到
我已經嘗試過不同的東西:
- 重新編譯和部署應用程序
- 移動的persistence.xml到不同的文件夾
- 經過錯誤日誌和調試的整個應用程序,無法找到其原因
- 只好看看所有可能的解決方案,我可以找到關於谷歌和嘗試他們...
我在這裏錯過了什麼?有任何想法嗎?
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 /類/ 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>
做任何查詢的工作,或者它只是你的命名查詢無法找到?嘗試設置啓動時部署持久性屬性以查看是否有幫助https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_deploy_on_startup.htm – Chris
正如您在我的答案中所看到的,我可能已經解決了我的問題。在另一個班上有一個完美的查詢,只是在這個班上。但我仍然不知道爲什麼我的解決方法確實有效...... – Burdel
這就是我的評論 - 找出其他選項和潛在問題。如果你對你的解決方法很好,很好。 – Chris