# @Convert ignored when @Id is used

**URL:** https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281
**Category:** Hibernate ORM
**Created:** [February 13, 2019, 4:47pm UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281 "2019-02-13T16:47:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sgnik](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sgnik](https://discourse.hibernate.org/u/sgnik)
#### Post date: [February 13, 2019, 4:47pm UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/1 "2019-02-13T16:47:11Z")

</div>

I have an entity with a composite id:

```auto
@Entity
public class Entity {
  @Id
  private Long idnr;

  @Id
  private LocalDateTime timestamp;

  // Getters/setters omitted
} 

```

I want to use a custom JPA converter for the LocalDateTime field (due to a bug in handling timezone by the default converter). This works as expected when @Id is removed from the LocalDateTime field, but the converter is ignored when the Id is present.

I tried both an auto-applied and an explicit (@Convert(converter=Converter.class) converter.

How can I get the converter working on a field with @Id

TIA,  
SH

---

<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: [February 13, 2019, 5:00pm UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/2 "2019-02-13T17:00:18Z")

</div>

Try to use an Embeddable for the composite id. Defining multiple @Id is not a very good idea since the entity itself is both the entity and the identifier, and you need to implement Serializable amd provide equals and hashCode. These requirements are not needed for EmbeddedId.

---

<div class="post-metadata">

### Author: ![sgnik](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sgnik](https://discourse.hibernate.org/u/sgnik)
#### Post date: [February 21, 2019, 9:15am UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/3 "2019-02-21T09:15:09Z")

</div>

Thanks for the answer.

Unfortunately using an embeddable id is not an option at the moment.

Is there another way to make the converter work on an @Id field? And is this expected behaviour or is this a bug?

---

<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: [February 21, 2019, 9:49am UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/4 "2019-02-21T09:49:52Z")

</div>

You need to check the JPA spec and see whether the AttributeConverter is supposed to work for the entity identifier.

If that’s the case, then this could be an issue.

---

<div class="post-metadata">

### Author: ![sgnik](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sgnik](https://discourse.hibernate.org/u/sgnik)
#### Post date: [February 23, 2019, 6:27am UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/5 "2019-02-23T06:27:30Z")

</div>

According to the JPA 2.2 spec (10.6) this is indeed intended behaviour:  
“Note that Id attributes, version attributes, relationship attributes, and attributes explicitly annotated as  
Enumerated or Temporal (or designated as such via XML) will not be converted.”

In 3.8 the following can be found:  
“The conversion of all basic types is supported except for the following: Id attributes (including the  
attributes of embedded ids and derived identities), version attributes, relationship attributes, and  
attributes explicitly annotated as Enumerated or Temporal or designated as such in the XML  
descriptor. Auto-apply converters will not be applied to such attributes, and applications that apply converters to such attributes through use of the Convert annotation will not be portable.”

So the converter not working for an @Id annotated field is desired behaviour. But the fact that the conversion seems to work on a field in an embedded id might not be.

However, I have found another solution for my problem, i.e. using a Hibernate user type. A user type can be applied to an @Id annotated field.

Thank you for your support

---

<div class="post-metadata">

### Author: ![twwwt](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@twwwt](https://discourse.hibernate.org/u/twwwt)
#### Post date: [March 6, 2020, 8:47am UTC](https://discourse.hibernate.org/t/convert-ignored-when-id-is-used/2281/6 "2020-03-06T08:47:20Z")

</div>

> [@sgnik](#):
>
> Is there another way to make the converter work on an @Id field? And is this expected behaviour or is this a bug?

Had the same question this week and found a solution that works, which is based on this [question on Stackoverflow](https://stackoverflow.com/q/44069361/3955765). It’s essentially based on using an `@IdClass`; i.e., having a composite primary key (which also applies to the example posted above). The trick is to place the `@Convert` annotation (and also the `@Column` annotation) on the field in the id class, not the field annotated with `@Id`:

```java
// minimum example
@Entity
@IdClass(SomeEntity.PrimaryKey.class)
public class SomeEntity
{
	@Id
	private MyEnum type;

	@Id
	private String entityId;

	// ...

	public static final class PrimaryKey
	{
		@Column(name = "\"type\"", nullable = false, updatable = false)
		// According to JPA, @Convert should not be used on @Id field (SomeEntity.type), see Section 11.1.10.
		// Hibernate ignores the annotation if done so. It works, however, when placed on the corresponding
		// field of an @IdClass, see also https://stackoverflow.com/q/44069361/3955765
		@Convert(converter = MyEnumConverter.class)
		private MyEnum type;

		@Column(name = "entity_id", nullable = false, updatable = false)
		private String entityId;

		// ...
	}
}

```

`MyEnum` is a custom enumeration. `MyEnumConverter` is an `AttributeConverter` that converts to/from a character (i.e., values of `MyEnum` are encoded by a character in the database). This also works when Hibernate creates the schema; that is, the type of the database field named `type` is correctly set to a character (not an integer as is the default for enumeration types).

Tested with Hibernate 5.3.13.

I’m just not sure whether this is defined behavior or whether it’s working by chance. Maybe someone from the Hibernate team can comment on this?
