1

So I am making a spring-boot application and I am importing some datamodel from an external library (imported through maven). I have some model that has a field of a type of the external library and I want to be able to persist it. Something like:

package com.example.module;
import external.Phone;
@Entity
public class Person{
  @Id
  @GeneratedValue
  String id;
  String name;
  Phone phone;
}

The question is, is it possible to store the Phone if it's implementation is something that is not JPA aware? E.g.:

package external;

public class Phone{
  String number;
  String areaCode;
}
LECHIP
  • 81
  • 1
  • 8

2 Answers2

1

Depends what you want to do with it in terms of persistence.

If you wanted Phone to be an Entity in its own right you can define an orm.xml file to specify its persistence (and if your JPA provider uses bytecode enhancement you'd also have to be careful about getting an updated Java class file for it).

If instead you just want to persist it embedded then look at creating a JPA AttributeConverter and persist it into the same table as the owner. Your JPA provider may or may not permit persisting it into multiple columns via the AttributeConverter (DataNucleus JPA, the one I use, supports this flexibility but this is a vendor extension and may not be available in yours).

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • I followed (sor tof) your second advice, using `@Convert` along a ORM mapper class. I really want to avoid doing any kind of xml configuration, since it always feels like a step back from the convention over configuration annotation approach that I want to enforce on my app. – LECHIP Nov 29 '15 at 18:14
0

I ended up using this approach JPA map JSON column to Java Object, since it allows me full control on how to persist the field, even to a string with the JSON of the object I wanted.

Community
  • 1
  • 1
LECHIP
  • 81
  • 1
  • 8