jakartaee / jakartaee/persistence

Support @Convert specification on id attributes

Open
#207 12 comments 16 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
268
Forks
78
Avg merge
1d 6h
Merged PRs (30d)
13

Description

We want to map an id attribute to an immutable type.

```java
class FooId implements Serializable {
private final int value;

private FooId(int value) {
this.value = value;
}

public static FooId of(int value) {
return new FooId(value);
}

public int value() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FooId fooId = (FooId) o;
return value == fooId.value;
}

@Override
public int hashCode() {
return Objects.hash(value);
}
}

class Converter implements AttributeConverter {

@Override
public Integer convertToDatabaseColumn(FooId attribute) {
return Optional.ofNullable(attribute).map(FooId::value).orElse(null);
}

@Override
public FooId convertToEntityAttribute(Integer dbData) {
return Optional.ofNullable(dbData).map(FooId::of).orElse(null);
}
}

@Entity
class Foo {
@Id
@Convert(converter = Converter.class)
@Column(definition = "integer")
private FooId id;
}
```

Hibernate 5 fails to convert the id to the immutable type.

Reading the JPA 2.1 spec, we found this:

>The Convert annotation should not be used to specify conversion of the following: Id attributes
(including the attributes of embedded ids and derived identities), version attributes, relationship
attributes, and attributes explicitly annotated (or designated via XML) as Enumerated or Temporal. Applications that specify such conversions will not be portable

Would it be possible to reconsider this to make `convert on id attributes` a use case part of the next version of JPA?

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the issue's immutable FooId and AttributeConverter example alongside the quoted JPA 2.1 restriction on Id attributes. Determine whether Jakarta Persistence should support @Convert on id attributes and what specification change would be required; completion would require a decided, actionable scope rather than only reconsideration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.