# Set Primary key and Foreign key on a single column in hibernate mapping having type String

**URL:** <https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108>\
**Category:** Hibernate ORM\
**Created:** [January 22, 2018, 9:33am UTC](https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108 "2018-01-22T09:33:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranav](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Pranav](https://discourse.hibernate.org/u/Pranav)\
**Post date:** [January 22, 2018, 9:33am UTC](https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108/1 "2018-01-22T09:33:20Z")

</div>

I have 2 tables - `Request` and `SubRequest`.

They both need to have a one-to-one mapping. In the `SubRequest` table I need to have the Primary Key and the Foreign key on the same column (which is RequestID) .

It is easily done in DB. My application uses Hibernate. So in the hibernate mappings file I need to do the same.

Also, the requirement is that this key must be a VARCHAR and generated by the application and not auto-generated by DB. I tried doing it from the following:

- [http://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-with-primary-key-xml-mapping-example](http://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-with-primary-key-xml-mapping-example)
- [http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/](http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/)

But all of these have the id as Integer and auto-generated.

I could not make it work for RequestID as String. Is it possible to do this in hibernate?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 22, 2018, 9:54am UTC](https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108/2 "2018-01-22T09:54:28Z")

</div>

> [@Pranav](#):
>
> I could not make it work for RequestID as String. Is it possible to do this in hibernate?

Of course, it is possible.

> I need to have the Primary Key and the Foreign key on the same column (which is RequestID) .

First, you need to use `@MapsId` as explained in [this article](https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/).

> Also, the requirement is that this key must be a VARCHAR and generated by the application and not auto-generated by DB.

Just remove the `@GeneratedValue` fro the `@Id` in the `Request` class:

```auto
@Id
@Column(name = "RequestID")
private String id;

```

And in the `SubRequest` entity you will have:

```auto
@Id
@Column(name = "RequestID")
private String id;

@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Reqeust request;

```

---

<div class="post-metadata">

**Author:** ![Pranav](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Pranav](https://discourse.hibernate.org/u/Pranav)\
**Post date:** [January 23, 2018, 6:24am UTC](https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108/3 "2018-01-23T06:24:18Z")

</div>

Thanks Vlad. I tried using the following approach. But I am getting the following error-

> ERROR: ORA-00904: “REQUEST\_REQUEST\_ID”: invalid identifier

This is coming when inserting a row in SubRequest table. It seems it is trying to find REQUEST\_REQUEST\_ID column instead of the REQUEST\_ID. I found that hibernate is getting this column name by taking the @MapId variable name and appending it with the column name which we have given in the @Column. Ideally it should have just used the name mentioned in the @Column right?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 23, 2018, 8:51am UTC](https://discourse.hibernate.org/t/set-primary-key-and-foreign-key-on-a-single-column-in-hibernate-mapping-having-type-string/108/4 "2018-01-23T08:51:01Z")

</div>

Try it like this and it will work:

```auto
@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "RequestID")
private Reqeust request;

```

For more details, check out [this article](https://vladmihalcea.com/change-one-to-one-primary-key-column-jpa-hibernate/).
