Reputation: 41
I am having to write an application to communicate with a legacy Oracle 8i database. I am doing this using Spring Boot/JPA, but I have a question around mapping foreign key relationships. Usually with JPA I would use the annotation @JoinColumn
and reference the relevant columns, but as this database does not support all ANSI SQL keywords such as JOIN, I am not sure this would work. I have had success using the (+)
operator to join tables with manual SQL statements, but I do not know if I could configure JPA to use this behaviour?
For example:
@Data
@Entity
public class MyFirstTable {
@Id @Column(name = "ID")
private int id;
@ManyToOne
@JoinColumn(name = "REFERENCED_KEY", referencedColumnName = "ID")
private MySecondTable mySecondTable;
}
@Data
@Entity
public class MySecondTable {
@Id @Column(name = "ID")
private int id;
}
This would be my usual way of mapping this type of relationship in JPA.
The only other way I can think of is if I have to just have the referenced PK mapped as a simple @Column
, and manage the relationship myself?
For example:
@Data
@Entity
public class MyFirstTable {
@Id @Column(name = "ID")
private int id;
@Column(name = "REFERENCED_KEY")
//Manage reference outside of the Entity class
private int mySecondTableKey;
}
@Data
@Entity
public class MySecondTable {
@Id @Column(name = "ID")
private int id;
}
I would rather avoid this if possible, so I was wondering what my options are.
Also, I do end up having to delve into using composite primary keys from some tables as a foreign key within a composite primary key of another, and I'd rather avoid any potential ambiguity of a relationship where this is the case. Several entries in some tables also have not been configured correctly, where they reference a separate table as they would a foreign key, but it has never been correctly constrained. So I would need to be validating that these entries are correct within their own table before using them within any INSERT statements, so I do not insert an ID that does not actually exist.
Upvotes: 1
Views: 253