0

Let’s say we have a User

@Entity
class User {
    String name;
    …
    ExternalSystemObject eso;
    …
}

Is there an easy way to store the ExternalSystemObject as it’s identifier only (e.g. a column eso_id: String for example) and provide a mapper to map the ID back to an object when retrieving the data?

Obviously I can define the field as String in the model but that would involve creating another object to represent the original required data model.

I thought that is a simple commonly desired behaviour but couldn’t find anything in the documentation.

1 Answer 1

1

Found a way to do that using a javax.persistence.Converter;.

@Converter
public class ExternalSystemObjectConverter implements AttributeConverter<ExternalSystemObject, String> {

    @Override public String convertToDatabaseColumn(ExternalSystemObjectConverter eso) {
        return eso.getId();
    }

    @Override public ExternalSystemObjectConverter convertToEntityAttribute(String dbData) {
        // retrieve or build eso
        return eso;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.