red
JCHJava Community | Help. Code. Learn.
•Created by red on 2/8/2025 in #java-help
@MapsId not works
I have two entities as below, and I would like the "cart" entity to have the same id as the Customer entity of its respective relationship
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Customer extends User {
public Customer(LoginAndRegisterDto dto) {
super(dto);
}
@OneToOne(cascade = CascadeType.ALL)
private Cart cart;
@PrePersist
public void generateCart(){
if(cart ==null){
Cart carro = new Cart();
cart=carro;
cart.setCostumer(this);
}
}
}
The cart Entity: @Entity
@Getter
@Setter
public class Cart {
@Id
private Long id;
@OneToOne(mappedBy = "cart")
@MapsId
@JoinColumn(name = "id")
private Customer costumer;
@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},orphanRemoval = true
,mappedBy = "cart")
private Set<Order> orders;
}
37 replies
JCHJava Community | Help. Code. Learn.
•Created by red on 1/16/2025 in #java-help
Help me with spring JDBC

56 replies