'JPA relationship for existing table and parent table
I have a table which has a foreign key relationship with an existing table and its child table. The parent table exists before the child table as is populated by a liquibase script. The tables are as as follows:
person
id int primary key,
name varchar2(25) not null,
age int not null
child
id int primary key,
name varchar2(25) not null,
person_id_fk int references person(id)
Is it possible to omit the relationship between the person and child entities and only specify the foreign key in the child entity class. Since the Person table has already been populated, I don't need to save rows to it. I simply want to enforce the relationship by the database tables definitions and have the dbms enforce it. In other words, when I save the child, I only want the dbms to enforce the integrity constraints if they are violated. Is this possible to simply express the entity like shown below and not as a @ManyToOne in the child table?
@Entity
class Child {
int id;
String name;
int personFk; // foreign key in the person table.
}
Solution 1:[1]
I simply added a field to the object representing the foreign key relationship, as in my child object and enforced the foreign key relationship at the database level and I encountered no problems
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BreenDeen |