简体   繁体   English

JPA命名查询问题

[英]JPA NAMED Query issue

Getting below database excpetion, help required 深入了解数据库专有技术,需要帮助

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 Method DAO方法

public TermPayment findFirstPaymentByTotalAndPlanId(int planId, double totalAmount) { 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();
}

Return class 回程班

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

From the java.persistence.EntityManager javadoc: 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);

so you should create your named query and only refer to it using createNamedQuery . 因此,您应该创建命名查询,并且只能使用createNamedQuery进行createNamedQuery

If you want to create a query from a string, you can use the createQuery(String query, Class type) method. 如果要从字符串创建查询,则可以使用createQuery(String query,Class type)方法。

You can replace the method used in your DAO: 您可以替换DAO中使用的方法:

entityManager.createNamedQuery(...)

For this one: 为此:

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

Alternatively, you can create a NamedQuery adding a NamedQuery annotation in your Entity class or using xml. 或者,您可以创建一个NamedQuery,在您的Entity类中或使用xml添加一个NamedQuery批注。 Afterthat, you could use the NamedQuery passing the NamedQuery name to the createNamedQuery() method. 之后,您可以使用NamedQuery将NamedQuery名称传递给createNamedQuery()方法。

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

entityManager.createNamedQuery(MyQuery, TermPayment.class);

You are trying to use NamedQuery api for creating dynamic queries which is wrong. 您正在尝试使用NamedQuery api创建dynamic queries ,这是错误的。 From 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();

You need to do something like, 你需要做类似的事情,

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