JPA / OpenJPA: Map integer field from DB table to custom object instead of enum

20 views Asked by At

I use OpenJPA in my Java project. The question is about a non-nullable integer column.

Currently this field is mapped to an enum class by the ordinal index:

@Column(nullable=false)
@Enumerated(EnumType.ORDINAL)

Instead, I'd like to map this db-side integer column to a custom object.

Something like

@Column(nullable=false)
@CustomObject( dbtype=java.sql.Types.INTEGER,  // What do we find in the DB table?
               intValue -> MyObjectsRepository.getInstance( intValue ),  // Integer to MyObject
               myObject -> myObject.getIntValue() )  // MyObject to Integer

Does this exist?

In the best case, there is a JPA-way (implementation agnostic). But if there's only an OpenJPA-only trick, that would help me as well.

0

There are 0 answers