@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; }
25 Replies
JavaBot
JavaBot•3w ago
⌛ This post has been reserved for your question.
Hey @red! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot•3w ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
red
redOP•3w ago
stack.txt
red
redOP•3w ago
The full stacktrace
dan1st
dan1st•3w ago
Does it wirk without inheritance?
red
redOP•3w ago
I wish it worked with inheritance
Suika
Suika•3w ago
The User abstract entity: @Getter @Setter @Entity @NoArgsConstructor @Inheritance(strategy = InheritanceType.JOINED)
public abstract class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Getter
private Long id;
@Column(unique = true)
private String email;
private String password;

public List<Role> roles;
public User(LoginAndRegisterDto dto) {
this.email=dto.email();
this.password=dto.password();
}
public boolean isAdmin(){
return this.roles.contains(Role.ADMIN);
}

}
public abstract class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Getter
private Long id;
@Column(unique = true)
private String email;
private String password;

public List<Role> roles;
public User(LoginAndRegisterDto dto) {
this.email=dto.email();
this.password=dto.password();
}
public boolean isAdmin(){
return this.roles.contains(Role.ADMIN);
}

}
This message has been formatted automatically. You can disable this using /preferences.
dan1st
dan1st•3w ago
try it without inheritance- if you know about whether or not that works, you are one step further or try private User customer in the @MapsId column
red
redOP•3w ago
It didn't work
dan1st
dan1st•3w ago
yeah I think the combination of inheritance with MapsId isn't supported by Hibernate, see also https://stackoverflow.com/a/68688543/10871900
Stack Overflow
Hibernate joined table inheritance im combination with OneToOne and...
Using Hibernate in combination with mysql. When I try to create a @OneToOne Relationship from a child of a joined table inheritance while also using @MapsId to map the Id of the joined table child...
red
redOP•3w ago
The problem is that if I remove the inheritance between User and customer, I would have to refactor a lot of code. Oh Thanks, I think this saved me a few hours trying to debug this
JavaBot
JavaBot•3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1st•3w ago
Does that really need to be the primary key? Can't you specifically another primary key and just use a UNIQUE constraint on that field there?
red
redOP•3w ago
A basic identifier would be enough, not necessarily a primary key
dan1st
dan1st•3w ago
then you don't need @MapsId I guess but in general: Avoid inheritance with JPA
red
redOP•3w ago
Why? I got curious
dan1st
dan1st•3w ago
because it just causes issues all the time especially when it gets more complicated
red
redOP•3w ago
Well, I'll review that inheritance then Anyway, thanks for the help
dan1st
dan1st•3w ago
but for this if it's too much effort: you could still use inheritance but with a different @Id but no @MapsId
red
redOP•3w ago
How would it look?
dan1st
dan1st•3w ago
public class Cart {
@Id
private Long id;
//no @MapsId
@OneToOne(...)
private Customer customer;
}
public class Cart {
@Id
private Long id;
//no @MapsId
@OneToOne(...)
private Customer customer;
}
I left a few things out for simplicity you even have the @Id already
red
redOP•3w ago
No need for @GeneratedValue?
dan1st
dan1st•3w ago
left a few things out for simplicity
red
redOP•3w ago
Oh, I got it I'll try again
JavaBot
JavaBot•3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?