I am looking to map a field in the owner entity to the ID of the target
entity.
For example, taking the Employee -> Department mapping:

class Employee {

private String name ;

private String depId ;

private Department department ;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "dep_id", referencedColumnName = "id", insertable =
false, updatable = false)
public Department getDepartment(){
return this.department ;
}


public void setDepId(String depId){
this.depId = depId;
}
...

}

My Question is, how can I set the Deparment by setting only its dep_id in
the employee object, and keep the foreign key constraints when the tables
are created ??

For example:

Employee emp = new Employee() ;
emp.setId("SOME_ID_1") ;
emp.setDepId("FINANCE");

em.persist(emp);

and if there's no FINANCE record in the department table, then we have an
constraints violation.

 At the same time I don't want to have setters for Department Entity. Only
its ID.

Thank you.

Reply via email to