Home > Enterprise >  Spring JPA: change doesn't persist in OneToOne relationship
Spring JPA: change doesn't persist in OneToOne relationship

Time:03-18

I use Spring Boot Starter Data JPA for my thesis, and I have a bug, I can't quite work out.

I have the following relationship between my entitites:

Restaurant

@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "restaurants")
public class Restaurant {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    // ...

    @OneToMany(targetEntity = RestaurantTable.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "restaurant_table_fk", referencedColumnName = "id")
    private Set<RestaurantTable> restaurantTables;
    // ...
}

RestaurantTable

@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "restaurant_table")
public class RestaurantTable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id

    // ...

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "restaurant_table_id")
    private EndUser user;

    // ....

}

EndUser

@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "end_users")
public class EndUser {
    public EndUser(String userId, String encryptedPassword) {
        this.userId = userId;
        this.encryptedPassword = encryptedPassword;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;
    private String userId;
    private String encryptedPassword;
}

Basically: Every restaurant can have many tables, and each and every table has a corresponding user.

When I create a new table I automatically create a new user for it.

RestaurantTableService

// ...

@Override
public void add(RestaurantTable restaurantTable) {
    var restaurant = getRestaurant();

    restaurantTable.setUser(
            new EndUser(idGenerationService.get(), passwordEncoder.encode(passwordGenerationService.get()))
    );

    restaurant.addTable(restaurantTable);
    restaurantRepository.save(restaurant);
}

// ...

This works so far. The problem is when I try to change the password of the user of a table. I tried more approaches.

create a new user with updated password and assign it to a table

var newPassword = passwordGenerationService.get();
var user = restaurantTable.getUser();
var newUser = new EndUser(user.getUserId(), passwordEncoder.encode(newPassword));

restaurantTable.setUser(newUser);
restaurantTableService.update(restaurantTable);

update the existing user

var newPassword = passwordGenerationService.get();
var user = restaurantTable.getUser();

user.setEncryptedPassword(passwordEncoder.encode(newPassword));

restaurantTable.setUser(user);
restaurantTableEntityService.update(restaurantTable);

restaurantTableEntityService.update(restaurantTable) looks like this:

RestaurantTableService

@Override
public void update(RestaurantTable restaurantTable) {
    var restaurant = getRestaurant();
    restaurant.updateTable(restaurantTable);
    restaurantRepository.save(restaurant);
}

None seems to work. The change doesn't persist. What should I change to make the password change persist?

CodePudding user response:

Just use the direct repository to persist the changes you want directly

var newPassword = passwordGenerationService.get();
var user = restaurantTable.getUser();
user.setEncryptedPassword(passwordEncoder.encode(newPassword));
endUserRepository.save(user);

CodePudding user response:

persist the change into the user entiy : endUserRepository.save(user);

  • Related