I would like to rewrite the stored value for a @ManyToOne relationship before writing to the database to mimic the behaviour of some legacy code. Latter stores a unset relationship as 0 instead of null. All other values (>0) are regular references.
Is there a way to implement a custom mapping for the @ManyToOne value to handle this?
Entities:
@Entity
@Table(name = "Content")
public class ContentEntity
// ..
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ColorID")
private ColorEntity color;
}
@Entity
@Table(name = "Color")
public class ColorEntity {
@Id
@GeneratedValue
@Column(name = "ID")
private String id;
// ..
}
Database:
CREATE TABLE `Content` (
# ..
`ColorID` VARCHAR(255) DEFAULT NULL);
CREATE TABLE `Color` (
`ID` VARCHAR(255) PRIMARY KEY,
# ..)
There is no corresponding entry in the target table with id:0 but multiple records for id:>0
I'm using JPA in a Spring Boot application.