Home > Net >  Java Repository can't implement method
Java Repository can't implement method

Time:12-11

I am using the Micronaut framework, and I have the following setup: have an Entity called Transaction, with a Long ID, and three fields, a UUID, a String Report and a String State:

@Entity
public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


private UUID uuid;

private String report;

private String state;
}

In a CrudRepository, I have defined the following query:

int updateByUuid(UUID uuid, String state);

This correctly updates my Transaction entity with the given new State. Now I wanted to write a different query:

int updateByUuid(UUID uuid, String report):

Obviously this failes to build, since the signature is the same. I tried to write the following:

int updateReportByUuid(UUID uuid, String report);

or:

int updateReportByUuid(String report, UUID uuid);

(So changing the parameters around) This fails as well, with the error message of

Projections are not supported on batch updates

and

No possible implementations found

The only time it did not fail to build is when I changed the function to the following:

int updateReportByUuid(@Id Long id, String report);

But obviously this is incorrect (since I specified ByUuid, and as the first parameter I have given a Long id), and also not the functionality I want. What I want is for the database to find the Transaction I want to update by its' UUID, and update its' Report column.

CodePudding user response:

The reason why this is considered as a batch update is, because you are updating records on a non primary key field.

As mentioned above the field uuid is technically unique and you also declared it as unique but it is not a primary key. Therefore Micronaut Data considers it as a batch update.

You have at least the following two options.

  • Convert the id field from Long to UUID and drop the field uuid. With that case your primary key is a UUID and your update methods will start to work.
  • Tell Micronaut Data what to do by adding at @Query annotation.
  • Related