A
A
adreSsS2021-04-10 17:45:48
Spring
adreSsS, 2021-04-10 17:45:48

Spring Data JPA - JPQL Update query?

I have a specific exercise as part of my homework in class at the university. This entity structure looks like this:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Parent {
...some fields...
}

@Entity
public class ChildA extends Parent {
...
}

@Entity
public class ChildB extends Parent {
...
}

I need to implement the JpaRepository method on ParentRepository or rather JPQL Query to perform an update operation on the table given in Class<? expands the Parent> parameter. It's just that the method should update some field, but only in the subclass given as a parameter.

The structure of the method should be:
public interface ParentRepository extends JpaRepository<Parent, Integer> {

    void updateInBulk(Class<? extends Parent> clazz, int someParameter);
}

I came up with something like this but it didn't work.
public interface ParentRepository extends JpaRepository<Parent, Integer> {

    @Modifying
    @Query("update :entity e set e.some_field = 'T' where e.other_field > :some_param")
    void updateInBulk(@Param("entity") Class<? extends Parent> clazz, @Param("some_param") int someParam);
}


It was just my blind shot, but I couldn't find any material that explains how to do this kind of thing.
Does anyone have any suggestions how to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-04-10
@BorLaze

Is it possible to specify the table name as a parameter?
As far as I know, this is only allowed for table fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question