问题
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