jakartaee / jakartaee/persistence
Allow AttributeConverter to be used for composite types by using @Embeddable
- 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
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