简体   繁体   English

动态JPA查询不起作用

[英]Dynamic JPA query not working

I am using join query through createQuery in JPA. 我在JPA中通过createQuery使用联接查询。 I have 2 tables MasterScrip and OrderMaster . 我有2个表MasterScripOrderMaster Entity code is given below. 实体代码如下。 The dynamic query is returning collection object. 动态查询正在返回集合对象。 I debugged and found that the query executes and returns collection object correctly; 我调试发现查询正确执行并返回集合对象。 but, after the object returned an error, shown below: 但是,在对象返回错误后,如下所示:

   [javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.]
    javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException...

    SEVERE: Error Rendering View[/ClientTemplate/orderReport.xhtml]
    javax.el.ELException: /ClientTemplate/orderReport.xhtml @14,142 value="#{stockOrderBean.scripLst}": com.sun.xml.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://service/}getOrderScripByUserNameResponse but found: {http://schemas.xmlsoap.org/soap/envelope/}Envelope
        at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)

Stateless bean method: 无状态bean方法:

 public Collection<MasterScrip> getOrderScripByUserName(String userName)
{        
    try
    {
        String squery = "select DISTINCT(s.scripSymbol),s.scripID from MasterScrip s,OrderStock o where o.scripID.scripID = s.scripID and o.userName.userName = '" + userName + "'";
        Collection<MasterScrip> c = em.createQuery(squery).getResultList();
        //UserMaster um = em.find(UserMaster.class,userName);
        return c;
    } catch(Exception e) {
        System.out.println(e);
        return null;
    }
}

What is the cause of this error? 此错误的原因是什么? How do I solve it? 我该如何解决?

First, as noted in the comments, you should always use parameters instead of concatenating parameter values: 首先,如注释中所述,应始终使用参数而不是连接参数值:

select DISTINCT(s.scripSymbol), s.scripID from MasterScrip s, OrderStock o 
where o.scripID.scripID = s.scripID and o.userName.userName = :userName

This would prevent SQL injection attacks, or simply incorrect queries in case of a user name like O'Reilly for example. 这样可以防止SQL注入攻击,或者防止诸如O'Reilly这样的用户名出现错误查询。

Your query returns two different columns. 您的查询返回两个不同的列。 There is no way for such a query to magically return instances of MasterScrip . 这样的查询无法神奇地返回MasterScrip实例。 It returns a List<Object[]> , where each Object[] contains two values: the scripSymbol and the scripID . 它返回List<Object[]> ,其中每个Object[]包含两个值: scripSymbolscripID

The query would return instances of MasterScrip if it was 该查询将返回MasterScrip的实例,如果它是

select distinct s from MasterScrip s, OrderStock o 
where o.scripID.scripID = s.scripID and o.userName.userName = :userName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM