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

**URL:** <https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901>\
**Category:** Hibernate ORM\
**Created:** [January 2, 2021, 10:53am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901 "2021-01-02T10:53:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 2, 2021, 10:53am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/1 "2021-01-02T10:53:21Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 2, 2021, 5:24pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/2 "2021-01-02T17:24:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 7:54am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/3 "2021-01-03T07:54:59Z")

</div>

Here you go:

> **[OneToOne - Pastebin.com](https://pastebin.com/6ESqEsxA)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 3, 2021, 11:41am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/4 "2021-01-03T11:41:17Z")

</div>

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)](https://docs.oracle.com/javaee/5/api/javax/persistence/PrimaryKeyJoinColumn.html)

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:

```auto
@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?

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 11:57am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/5 "2021-01-03T11:57:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 3, 2021, 12:24pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/6 "2021-01-03T12:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 12:45pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/7 "2021-01-03T12:45:29Z")

</div>

> [@beikov](#):
>
> ```auto
> @Enumerated
> private OrderStatus status;
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 3, 2021, 12:58pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/8 "2021-01-03T12:58:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 1:56pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/9 "2021-01-03T13:56:10Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 3, 2021, 3:07pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/10 "2021-01-03T15:07:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 3:45pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/11 "2021-01-03T15:45:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 3, 2021, 3:57pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/12 "2021-01-03T15:57:40Z")

</div>

Did you try the mapping I posted?

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 3, 2021, 4:30pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/13 "2021-01-03T16:30:06Z")

</div>

> [@beikov](#):
>
> ` @OneToOne(cascade = CascadeType.ALL, optional = false)`

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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 4, 2021, 8:30am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/14 "2021-01-04T08:30:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 4, 2021, 8:54am UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/15 "2021-01-04T08:54:55Z")

</div>

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?

> **[2021-01-04 17:08:44.948 DEBUG 22112 --- \[nio-8090-exec-3\]...](https://pastebin.com/jqMZGGze)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

Tks.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 5, 2021, 12:00pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/16 "2021-01-05T12:00:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karengsh](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@karengsh](https://discourse.hibernate.org/u/karengsh)\
**Post date:** [January 6, 2021, 1:13pm UTC](https://discourse.hibernate.org/t/org-hibernate-mappingexception-repeated-column-in-mapping-for-entity-for-onetoone/4901/17 "2021-01-06T13:13:40Z")

</div>

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.
