# @IdClass and @GeneratedValue not working in Hibernate

**URL:** <https://discourse.hibernate.org/t/idclass-and-generatedvalue-not-working-in-hibernate/843>\
**Category:** Hibernate ORM\
**Created:** [May 25, 2018, 10:53pm UTC](https://discourse.hibernate.org/t/idclass-and-generatedvalue-not-working-in-hibernate/843 "2018-05-25T22:53:03Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [May 26, 2018, 12:45pm UTC](https://discourse.hibernate.org/t/idclass-and-generatedvalue-not-working-in-hibernate/843/2 "2018-05-26T12:45:48Z")

</div>

`@IdClass` and `@GeneratedValue` works properly in Hibernate. We have a [test in the documentation folder](https://github.com/hibernate/hibernate-orm/blob/ceaeb81e3362ff187004ea3479b2afeeba5aa8a6/documentation/src/test/java/org/hibernate/userguide/mapping/identifier/IdClassGeneratedValueTest.java#L77) that proves it.

The problem in your code comes from this mapping:

```
@ManyToOne
private Samplings sampling;

```

The `Samples` entity will expect a samplings\_id FK to reference the `Samplings` table PK. But the `Samplings` table has a composite identifier.

Therefore, you need to use `@JoinColumns`:

```
@ManyToOne
@JoinColumns({
	@JoinColumn(
		name = "samplings_id",
		referencedColumnName = "id"),
	@JoinColumn(
		name = "samplings_year",
		referencedColumnName = "year")
})
private Samplings sampling;

```

For more details about mapping composite identifiers, check out the [User Guide](http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-nonaggregated).

---

_[View the full topic](https://discourse.hibernate.org/t/idclass-and-generatedvalue-not-working-in-hibernate/843)._
