jakartaee / jakartaee/persistence

Allow AttributeConverter to be used for composite types by using @Embeddable

Open
#105 27 comments 15 reactions 0 assignees View on GitHub
candidate-for-4.1 Priority: Major Type: New Feature
Dominant language
Java
Stars
268
Forks
78
Avg merge
1d 6h
Merged PRs (30d)
13

Description

Right now, AttributeConverters can only be used for single-value types. They could be extended to use an @Embeddable type as a surrogate column type for composite types.

Imagine the immutable type Money(final BigDecimal amount, final Currency currency) and the following @Embeddable type:

```java
@Embeddable
public class EmbeddableMoney {

private BigDecimal amount;
private Currency currency;

// getters & setters
}
```
```java
@Converter(autoApply = true)
public class MoneyConverter implements AttributeConverter {

@Override
public EmbeddableMoney convertToDatabaseColumn(Money attribute) {
if (attribute == null) {
return null;
}
EmbeddableMoney columns = new EmbeddableMoney();
columns.setAmount(attribute.getAmount());
columns.setCurrency(attribute.getCurrency());
return columns;
}

@Override
public Money convertToEntityAttribute(EmbeddableMoney dbData) {
if (dbData == null) { // check if empty? return null;
}
return new Money(dbData.getAmount(), dbData.getCurrency());
}
}
```
```java
@Converter(autoApply = true)
public class CurrencyConverter implements AttributeConverter {
...
}
```

What do you think?

Contributor guide

Open the contributing guide

Research direction

Start with the issue's AttributeConverter, @Embeddable, Money, and EmbeddableMoney examples, then review the 27-comment discussion for an agreed direction. Done means the proposed composite-type behavior and null/empty semantics are resolved clearly enough to define the required persistence changes; no file or test entry point is named in the issue.

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.