org.hibernate.MappingException: Repeated column in mapping for entity: for @OneToOne

Hi experts,
I am stuck as to how to do this, even though I have googled and tried out the below code but am not able to get it right.

Use case : if I create an order, the petId associate with the PetStatus cannot be sold

So in my order entity:
https://hastebin.com/onalekosax.kotlin

I would like to know how should I do the @OneToOne annotation while keep the int PetId there in the Order class and when I do my endPoint I will be able to check in the database if a PetId is there and if the PetStatus is sold. Cos I have the logic there already, but somehow it is not picking up the petId.

Tks.

Could you please post the code here or on a tool like https://pastebin.com which has a valid TLS certificate?

Here you go:

Please let me know what is the appropriate Annotations for the Order class to recognise the PetId and also the status of the Pet which is a Enum in Pet.

So, I am also wondering if any mapping is required at the Enum because the entity Order need to recognise certain status that it can’t create the Order if it reads that the status belong to certain status.

I have learnt from many tutorials that OneToOne is the way to go for my case but I am not sure why whenever I created a Oder endPoint in my controller and the PetId already in the database, it won’t recognise the PetId at all.

I have also tried to change my mapping to

@OneToOne(fetch = FetchType.LAZY)

But, this will not allow me to have the int Pet_Id in my Order class which I need it to check if the Pet_Id and the associate status before I create my Order via the endpoint in the Controller.

Tks.

I have no idea what you are trying to achieve, but since the primary key column is named id, you don’t need @PrimaryKeyJoinColumn. The documentation states this is only necessary when you use the primary key as foreign key: PrimaryKeyJoinColumn (Java EE 5 SDK)

If you want to map an enum, you have to use the @Enumerated annotation or a converter since an enum can be mapped in various ways in a database. Try the following:

@Table(name = "orders")
@Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    
    @OneToOne(cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "pet_id", referencedColumnName = "id")
    private Pet pet;
    
    private Date orderTimestamp;
    @Enumerated
    private OrderStatus status;
    private boolean complete;
}

Not sure if you are aware, but @OneToOne imposes that the join column, pet_id in this cases, is unique i.e. a unique constraint exists on the database, so there can only exist one Order per Pet. If you don’t want that, use @ManyToOne.

I have learnt from many tutorials that OneToOne is the way to go for my case but I am not sure why whenever I created a Oder endPoint in my controller and the PetId already in the database, it won’t recognise the PetId at all.

I have no idea what you mean by “recognise the PetId”, so could you please show me what you are doing?

Show the code, tell me what you want to achieve. What is it that doesn’t work? What are your expectations or what did you try so far?

Hi beikov,

I managed to get the Order to recognise my PetId now.

Only thing left now is I can’t get Order class to recognise the PetStatus - Enum in Pet class.
Could you advise me how should I go about doing my mapping for the Enum in Order class ?

And I will be using the code that I have posted earlier cos I need the pet_Id to be in.

Tks.

The pet_id is implicitly mapped through the primary key of the Pet object, so there is no need to map pet_id separately. I already wrote how you can do the mapping with the @Enumerated annotation.

Hi beikov,

I can’t do it as you said because I need to keep the pet_id as my logic need this field and I can’t do it implicity because that means eradicate my logic in my Service layer.

As for the Enum, I mentioned the petStatus in Pet class so what is the reason you are using orderStaus for the mapping instead?

I just used the code sample you posted… Anyway, like a mentioned already, you can just use the @Enumerated annotation for your PetStatus attribute.

You can access the pet id by using pet.getId(). Having a separate field is just redundant.

I can’t use pet.getId cos I need to make sure if my Order has this petId …

Furthermore, if I am going to removed the joinColumn and using implicit petId as you said, I will get errors :

model.Order column: pet_id (should be maped with insert=“false” update=“false”

You are getting this error because you map the column pet_id multiple times in the entity. Drop the second mapping.

what do you mean by I map the column pet_id multiple times. Other than the code with the mapping I have shown you, I have no other mappings anywhere as in the code base.

Please advise.

Did you try the mapping I posted?

I changed it to above.
But, it is worse now.
cos I get error
org.sprigframework.dao,DataIntegrityViolationException: could not execute statment; SQL[n/a]; constraint[null];
Cannot add or update a child row: a foreign key constraint fails

How is that worse? You are just inserting/updating invalid data. Apparently you use a not-null constraint for a column on the database side which is not fulfilled by the object you are trying to persist/merge. The complete error should have enough information to find which column is the problem and then you can try to avoid making the attribute that maps to that column null.

Hi beikov,

I managed to get rid of the earlier error but now I got a whole set of new errors.
Could you advise me what should I do to stop the complaints from Hibernate?

Tks.

You are getting a foreign key and a null constraint violation error, but there are so many reasons why this can happen. Since you are not sharing the code you are using, I can’t really see what is wrong with the way you are trying to use Hibernate.

I can only help you if you have a real question about Hibernate, but to me, it seems that you are lacking basic knowledge of SQL. In that case, you will first have to learn SQL to formulate a concrete question. I personally won’t “just solve” any random issue you are facing, especially if it’s not directly Hibernate related. Maybe someone else in the forum has the patience to teach you basic concepts.

Anyway, I would suggest you start by buying a book on SQL or follow one of the many online tutorials available on the internet and then read into Hibernate/JPA.

It is obvious that you don’t know the ansswer. But, that’s ok. Many other experienced ones I have asked, also don’t know. All the necessary codes are already given to you.

From the start, you are not able to get the gist of my question and then you turned around and said I need to know SQL basic. This is hibernate - it has nothing to do with SQL. Hibernate helps to match Java entities to Database.
You do not know. Period.