Rafael Lima
Rafael Lima

Reputation: 3535

JPARepository wont update entity on save

Very simple situation and JPA is killing my brain cells

 @Entity
@Table(name = "food_entry")
@ublic class FoodEntry implements Serializable {

    private static final long serialVersionUID = 1L;

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

    @Column(name = "product_name", nullable = false, unique = false, insertable = true, updatable = false, length = 256)
    private String name;

    @CreatedDate
    @Column(name = "instant", updatable = false, unique = false, nullable = false, insertable = true)
    private Instant instant = Instant.now();

    @Min(value = 0, message = "calories must be positive")
    @Column(name = "calories", updatable = false, insertable = true, nullable = false, unique = false)
    private long calories;

}

@Transactional
    public FoodEntry update(final FoodEntry newEntry, long id) {
        final User loggedUser = SecurityUtils.getCurrentLoggedUser();

        if (loggedUser == null || !loggedUser.getAuthorities().contains(Authority.ADMIN))
            throw new AccessDeniedException("You dont have authorization to perform this action");


        FoodEntry current = this.repository.findById(id).orElseThrow(() -> new NotFoundException("Not found FoodEntry with specified id: " + id));


        current.setCalories(newEntry.getCalories());
        current.setInstant(newEntry.getInstant());
        current.setName(newEntry.getName());

        try {
            this.repository.save(current);
            this.repository.flush();
            return null;
        }
        catch (Exception e) {
            throw e;
        }
    }

@Repository
public interface FoodRepository extends JpaRepository<FoodEntry, Long> {}

The code runs, the food entry is queried from database, but when i call save NOTHING HAPPENS, the JPA simple returns the very same object i passed as parameter and no query runs on database... later get to that entity will return the outdated value

why? this is so simple what am i missing?

The very same code for CREATE works fine... but when i'm trying to update, the save method does nothing

Upvotes: 0

Views: 17713

Answers (4)

Ashwini Karnale
Ashwini Karnale

Reputation: 350

The updatable = false attribute should be changed, JPA will not update the entities with attribute updatable as false so set it to true.

For more reference: https://www.ibm.com/support/pages/jpa-entities-primary-key-automatically-generated-read-only-annotations

Upvotes: 5

user10260739
user10260739

Reputation:

This problem came due to updatable=false, because whenever column is specified as updatable=false then it can not be updated through JPA.

Upvotes: 0

Manuel
Manuel

Reputation: 4228

The problem is, that all of those attributes you want to update (calories, instant, name) have set their updatable=false.

Attributes with updatable=false can only be set until the first time you have called .save(..). After that, all those attributes won't be updated anymore, even if the transaction hasn't been flushed.

Upvotes: 1

Rafael Lima
Rafael Lima

Reputation: 3535

I found the answer, is something super stupid I going to post here in case someone is stuck with same problem:

https://github.com/spring-projects/spring-data-jpa/issues/1735

JPA wont update entities in case all fields are set to update false, it does not throw an error or exception or any kind of traceable log, it simple ignores the call

as the project had an early requirement of not editing i forgot to alter the entities after it changed

Upvotes: 1

Related Questions