Spring Data - Can't update entity with @ManyToOne relations

Viewed 42

I got a simple SessionEntity:

public class SessionEntity {
...

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NonNull
    @ManyToOne
    @JoinColumn(name = "customer_id", nullable = false)
    CustomerEntity customerEntity;

...
}

And CustomerEntity:

public class CustomerEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NonNull
    private String territory;

    @NonNull
    private String code;

}

After the first transaction, I want to update SessionEntity with new CustomerEntity data using method:

@Query("update SessionEntity session "
    + "set session.customerEntity =:customerEntity "
    + "where "
    + "session.id =:sessionId")
Option<SessionEntity> updateCustomerForSession(@Param("sessionId") String sessionId, @Param("customerEntity") CustomerEntity customerEntity);

But nothing happen and in Postgres, I got still the same customer_id. Also, method one level above is public and @Transactional (SessionRepository - java class). What is wrong with this solution?

1 Answers

There are two things which must be fixed. First, as Igor mentioned, I must add @ Modifying and second, method updateCustomerForSession must return int or void.

Related