簡體   English   中英

JPA命名查詢問題

[英]JPA NAMED Query issue

深入了解數據庫專有技術,需要幫助

service() for servlet catalogservice threw exception: java.lang.IllegalArgumentException: Named query not found: SELECT OMX_PLAN_ID, PLAN_ID,DECODE(plan_id,0,ser_input_total_amount,first_payment) first_paymenmt From (SELECT OMX_PLAN_ID, PLAN_ID,(SELECT DECODE(fraction,0,fixed_payment_amount, (( fraction/100) * :useinput_total_amount)) From TFN.VW_OMX_PAYMENT_PLAN_DETAILS i WHERE o.OMX_PLAN_ID=i.OMX_PLAN_ID AND i.OMX_PLAN_ID=:omxPlanId AND i.PAYMENT_ID=1) first_payment FM TFN.VW_OMX_PAYMENT_PLANS o ) WHERE OMX_PLAN_ID=:omxPlanId ORDER by 1
        at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:704) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Fin

DAO方法

public TermPayment findFirstPaymentByTotalAndPlanId(int planId,double totalAmount){

    TypedQuery<TermPayment> query = entityManager.createNamedQuery("SELECT OMX_PLAN_ID, PLAN_ID,DECODE(plan_id,0,:user_input_total_amount,first_payment) first_paymenmt From (SELECT OMX_PLAN_ID, PLAN_ID,(SELECT DECODE(fraction,0,fixed_payment_amount, (( fraction/100) * :user_input_total_amount)) From TFN.VW_OMX_PAYMENT_PLAN_DETAILS i WHERE o.OMX_PLAN_ID=i.OMX_PLAN_ID AND i.OMX_PLAN_ID=:omxPlanId AND i.PAYMENT_ID=1) first_payment FROM TFN.VW_OMX_PAYMENT_PLANS o ) WHERE OMX_PLAN_ID=:omxPlanId ORDER by 1", TermPayment.class);
    query.setParameter("omxPlanId", planId);
    query.setParameter("user_input_total_amount", totalAmount);
    return   query.getSingleResult();
}

回程班

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class TermPayment {
    @Id
    @Column(name = "OMX_PLAN_ID")   
    Integer omxPlanId;

    @Column(name = "PLAN_ID")   
    Integer planId;

    @Column(name = "FIRST_PAYMENT")
    Double firstPayment;

    public Integer getOmxPlanId() {
        return omxPlanId;
    }
    public void setOmxPlanId(Integer omxPlanId) {
        this.omxPlanId = omxPlanId;
    }
    public Integer getPlanId() {
        return planId;
    }
    public void setPlanId(Integer planId) {
        this.planId = planId;
    }
    public Double getFirstPayment() {
        return firstPayment;
    }
    public void setFirstPayment(Double firstPayment) {
        this.firstPayment = firstPayment;
    }
}

java.persistence.EntityManager javadoc中:

/**
 * Create an instance of <code>Query</code> for executing a named query
 * (in the Java Persistence query language or in native SQL).
 * @param name the name of a query defined in metadata
 * @return the new query instance
 * @throws IllegalArgumentException if a query has not been
 *         defined with the given name or if the query string is
 *         found to be invalid
 */
public Query createNamedQuery(String name);

因此,您應該創建命名查詢,並且只能使用createNamedQuery進行createNamedQuery

如果要從字符串創建查詢,則可以使用createQuery(String query,Class type)方法。

您可以替換DAO中使用的方法:

entityManager.createNamedQuery(...)

為此:

entityManager.createQuery("select OMX_PLAN_ID, PLAN_ID ...", TermPayment.class)

或者,您可以創建一個NamedQuery,在您的Entity類中或使用xml添加一個NamedQuery批注。 之后,您可以使用NamedQuery將NamedQuery名稱傳遞給createNamedQuery()方法。

@NamedQuery(name="MyQuery", query="select OMX_PLAN_ID, PLAN_ID ...")

entityManager.createNamedQuery(MyQuery, TermPayment.class);

您正在嘗試使用NamedQuery api創建dynamic queries ,這是錯誤的。 來自文件

createNamedQuery method is used to create static queries, or queries that are defined in metadata by using the javax.persistence.NamedQuery annotation. The name element of @NamedQuery specifies the name of the query that will be used with the createNamedQuery method. The query element of @NamedQuery is the query:

@NamedQuery(
    name="findAllCustomersWithName",
    query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)

Here’s an example of createNamedQuery, which uses the @NamedQuery:

@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
    .setParameter("custName", "Smith")
    .getResultList();

你需要做類似的事情,

entityManager.createQuery( "SELECT c FROM Customer c WHERE c.name LIKE :custName") .setParameter("custName", name)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM