Call method in JPA
Asked Answered
C

5

15

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;
    }
}
Colleencollege answered 20/10, 2012 at 23:35 Comment(2)
Try: ... MyClass data data.x * 1000 = 100 - data.y in the query.Shaffer
No. It is just an example method. Indeed I have other method that is more complicated. But if I would be able to call myMethod() all problems will be solved.Colleencollege
R
5

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)

Rejoin answered 21/10, 2012 at 10:22 Comment(1)
This answer is incorrect, because ObjectDB does allow class' method usage inside query strings.Zymogenesis
Z
3

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();
Zymogenesis answered 4/1, 2019 at 22:44 Comment(1)
We can confirm that ObjectDB supports methods in queries as an extension to the standard JPQL. Class (static) methods and instance methods are supported. Note that in client-server mode class files with such methods have to be accessible to the server (i.e. in the server classpath).Guerdon
P
2

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.

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#EclipseLink_special_operators

Pringle answered 22/10, 2012 at 14:49 Comment(0)
P
-1

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.

Prisilla answered 21/10, 2012 at 0:2 Comment(1)
Which is clearly Hibernate-specific and the OP doesn't mention HibernateRejoin
C
-1

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().

Chimkent answered 21/10, 2012 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.