@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
⌛
This post has been reserved for your question.
Hey @red! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Please format your code to make it more readable. For java, it should look like this:
The full stacktrace
Does it wirk without inheritance?
I wish it worked with inheritance
The User abstract entity: @Getter
@Setter
@Entity
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.JOINED)
This message has been formatted automatically. You can disable this using
/preferences
.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
columnIt didn't work
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...
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
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.
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?
A basic identifier would be enough, not necessarily a primary key
then you don't need
@MapsId
I guess
but in general: Avoid inheritance with JPAWhy? I got curious
because it just causes issues all the time
especially when it gets more complicated
Well, I'll review that inheritance then
Anyway, thanks for the help
but for this if it's too much effort: you could still use inheritance but with a different
@Id
but no @MapsId
How would it look?
I left a few things out for simplicity
you even have the
@Id
alreadyNo need for @GeneratedValue?
left a few things out for simplicity
Oh, I got it
I'll try again
💤
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.