Reputation: 2565
I'm using ObjectDB with JPA. I would like to call myMethod(). For example:
entityManager.createQuery("SELECT ... FROM ... WHERE MyClass.myMethod() = 100")
Is it possible? Maybe any annotation is required before method in the class?
@Entity
public class MyClass implements Serializable {
@Basic
private int x;
@Basic
private int y;
public int myMethod() {
return x*1000+y;
}
}
Upvotes: 16
Views: 16182
Reputation: 3241
Yes, you can! And no additional annotations are required.
ObjectDB is an implementation of an object-oriented database system (OODBS) and as a result allows you to interact with database items as objects, that includes calling methods, using inheritance and polymorphism, etc.
This is a simple working example I have. With a class like this:
@Entity
public class Person {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
private long id;
private String firstName;
private String lastName;
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
This query returns correct results:
entityManager.createQuery(
"SELECT p FROM Person p WHERE p.getFullName()='John Johnson'", Person.class).getResultList();
Upvotes: 3
Reputation: 18379
JPQL is translated into SQL, so you cannot include a Java method call, as your database (most likely) does not support Java.
In JPA 2.1 you will be able to use the FUNCTION operator to call "database" functions. Some database do support defining functions in Java, but normally a proprietary database language is used (such as PL/SQL).
EclipseLink supports both FUNC and FUNCTION operators for calling database functions. You can also define your own operators using the OPERATOR operator which allows you to define your own custom database function call in Java.
Upvotes: 2
Reputation: 15577
JPQL is not exactly an object-based query language. You can't define your own methods, and JPQL provides a very limited set of functions. So if you want to keep within the JPA spec then the answer is no; would have to be JPA-implementation specific - DataNucleus JPA certainly allows you to have your own methods in the query language (as a vendor extension), no idea about your quoted JPA provider - that said though, it would only execute such a query in the datastore if you put the code for that method in a query method implementation (as opposed to in the class)
Upvotes: 5
Reputation: 19027
No you can't do that. Since JPA always works with prepared statement (parameterized SQL), you can only set parameters in the WHERE
clause of the JPQL query like
entityManager.createQuery("SELECT .... FROM ... WHERE
someCondition=:someValue).setParameter("someValue", "parameterValue");
Make the method (myMethod()
) return that value somehow and replace the second parameter which is parameterValue
of the setParameter()
method by the value returned by your method, myMethod()
.
Upvotes: -1
Reputation: 34367
Not the way you are looking for. If you want to use custom method, you can create and register them before using them.
e.g. create a function as below:
public int myMethod(int x, int y){
return x*1000+y;
}
Then register this function using registerFunction()
with the dialect
of your database. Once done, you can write a query as :
from MyClass data where myMethod(data.x, data.y) =100;
Hoe this helps.
Upvotes: -1