2

我试图从 postgresSql 函数中获取数据,我在这里使用了这个例子

在具有 IN/OUT 数据的映射类下方:

@XmlType(name="AccountRR")
@NamedStoredProcedureQuery
(
    name="getAccountMapping",
    procedureName="accountFunction",
    parameters = { 
                    @StoredProcedureParameter(name="in_route", mode=ParameterMode.IN, type=String.class),
                    @StoredProcedureParameter(name="in_round", mode=ParameterMode.IN, type=String.class)
            },
    resultSetMappings={"AccountRouteRoundMapping"}
)
@SqlResultSetMapping
(
    name = "AccountRouteRoundMapping",
    entities = 
    {
        @EntityResult
        (
            entityClass = AccountRouteRound.class,
            fields = 
            {
                @FieldResult(name = "name",         column = "name"),
                @FieldResult(name = "address_1",    column = "address_1"),
                @FieldResult(name = "address_2",    column = "address_2")
                [....]
            }
        )
    }
)
public class AccountRouteRound {.............}

在我调用 createNamedStoredProcedureQuery 的类下面

public static List<Account> getAccount(....)
{
    em.getTransaction().begin();
    StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("getAccountMapping");
    List CustomerRRDList = spq.getResultList();
    em.getTransaction().commit();
}

我收到了这个错误:

**Caused by: java.lang.IllegalArgumentException: No @NamedStoredProcedureQuery was found with that name : getAccountMapping**

我应该在哪里插入注释?怎么了 ?

谢谢

4

2 回答 2

3

固定的。我改变了我的代码:

public List<Account> getAccountFromStoreProcedure(EntityManager em, String routeCode, String round) 
{    
      StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("getAccountFunction");
    // set parameters
      storedProcedure.registerStoredProcedureParameter("in_route", String.class, ParameterMode.IN);
      storedProcedure.registerStoredProcedureParameter("in_round", String.class, ParameterMode.IN);
      storedProcedure.setParameter( "in_route",routeCode);
      storedProcedure.setParameter( "in_round",round);
      storedProcedure.executeUpdate();

      List<Object> objectList = storedProcedure.getResultList();

      for (int i = 0; i< objectList.size(); i++) {
          Account currAccount = new Account ((Object[]) objectList.get(i));
          tmpList.add(currAccount);
          System.out.println("currAccount : " + currAccount.toString()); }
    }

Account构造函数上:

public Account (Object[] fromStoredProcedure) {
        super();
        this.name           = (String)fromStoredProcedure[0];
        this.address_1      = (String)fromStoredProcedure[1];
        this.address_2      = (String)fromStoredProcedure[2];           
    }
于 2015-06-11T15:44:40.737 回答
0

我尝试过这种方式并且它正在工作,但是当函数发回一个列表时,我不知道如何获取所有组件并将其插入到列表中:

StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("getAccountMapping");
// set parameters
storedProcedure.registerStoredProcedureParameter("in_route",        String.class,     ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("in_round",        String.class,     ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("name",            String.class,     ParameterMode.OUT);
storedProcedure.registerStoredProcedureParameter("address_1",       String.class,     ParameterMode.OUT);
storedProcedure.registerStoredProcedureParameter("address_2",       String.class,     ParameterMode.OUT);
[...........]
storedProcedure.setParameter( "in_route",routeCode);
storedProcedure.setParameter( "in_round",round);
storedProcedure.execute();
String name = (String)storedProcedure.getOutputParameterValue("name");
[.......]
    System.out.println("TEST : "+name);
于 2015-04-17T13:08:21.970 回答