JPA NAMED查询问题

时间:2014-12-17 10:20:18

标签: java hibernate jpa

获取以下数据库重复,需要帮助

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;
    }
}

3 个答案:

答案 0 :(得分:0)

来自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引用它。

答案 1 :(得分:0)

如果要从字符串创建查询,可以使用createQuery(String query,Class type)方法。

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

entityManager.createNamedQuery(...)

对于这个:

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

或者,您可以在Entity类中创建NamedQuery或使用xml添加NamedQuery注释。之后,您可以使用NamedQuery将NamedQuery名称传递给createNamedQuery()方法。

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

entityManager.createNamedQuery(MyQuery, TermPayment.class);

答案 2 :(得分:0)

您正在尝试使用NamedQuery api来创建错误的dynamic queries。 来自doc

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)