I refactor some classes from standard SQL to JPA/ORM usage. In most cases the Objects have a "real" reference but sometimes the references to other objects are only given by an unchecked database id reference (no foreign key, just simple a string with a reference to another table id).
The code looks like:
@Entity
public final class myEntity implements Serializable {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "ID")
private String id;
@OneToOne
@JoinColumn(name = "OBJREF", nullable = false)
private otherObject objReference; /* Nice object reference */
@Column(name = "OTHEROBJREF")
private String otherObjReference; /* Damn db reference used by legacy code */
}
How should I deal with the otherObjReference? The attribute is used by legacy systems which need the static getter/setter construct with a String! If I stay with the ID I'll have problems with JPA queries and I can't simply assign an object and persist it.
I thought about making the String transient and work with @PrePersist and @PreLoad to load an "real" object reference so I can work with both. But in those methods I've no access to the EntityManager (and the persistence shouldn't be the task of this pojo in this case... it smells like bad design if I'll load references here).
Because the "otherObjReference" is private, I can also use getters and setters to load data from my real reference. But other layers work with the objects, so they'll fail when they call a getOtherObjReference() method because they've no db connection to load the object.