Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy

拟墨画扇 提交于 2019-12-24 01:11:19

问题


I am calling function from Oracle database and facing this Exception:

org.hibernate.engine.query.ParameterRecognitionException: Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy

This is my User.java entity.

@Entity
@Table(name = "users", schema = "myschema")
@javax.persistence.NamedNativeQuery(name = "getPass", query = "{? call = his.get_abc(:mrno)}", resultClass = User.class, hints = {
    @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
public class User {

@Id
@Column(name = "USERID", nullable = false)
private String userid;

@Column(name = "MRNO")
private String mrno;

private String username;
private String password;
private String fullName;
    // Getters and Setters are written.
}

And this is how I am calling this function from my one of Service class.

public boolean validateUser(String mrno, String password) {

    String completeMrno = utils.getMedicalRecordNumber(mrno);

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("his-dev");
    EntityManager entityManager = factory.createEntityManager();

    Query query = entityManager.createNamedQuery("getPass"); // <- this line is raising exception.
    query.setParameter("mrno",completeMrno); 
    List<?> results = query.getResultList();
 }

So, what changes are required in order to call Oracle Function which returns a String.

Thanks. Let me know if more information is required.


回答1:


Find below an example how to call the function with JPA:

Object result = entityManager.createNativeQuery("SELECT his.get_abc(:mrno) FROM DUAL")
                                      .setParameter("mrno", completeMrno)
                                      .getSingleResult();
String value = new String(result);


来源:https://stackoverflow.com/questions/54017772/mixed-parameter-strategies-use-just-one-of-named-positional-or-jpa-ordinal-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!