java - How to insert foreign key references into database with JPA? -
in company
model there employee
superclass (it has public setter
& getter
methods):
@entity @inheritance @discriminatorcolumn(name="emp_type") @table(name="employees") public abstract class employee implements printable, serializable { @id @generatedvalue @column(name = "employee_id", unique = true) private int id; @column(name = "position") @enumerated(enumtype.string) private position position; @column(name = "name") private string name; @column(name = "salary") private double salary; @transient public list<project> projectsworkingon = new arraylist<>(); public employee() { } }
there 2 subclasses worker, leader
extending employee
, , 5 other classes extending this, e.g.: developerleader
:
@entity @discriminatorvalue("dl") public class developerleader extends leader { public developerleader() { } }
i have project
class, has 3 worker
types:
@entity @table(name = "projects") public class project implements printable, serializable { @id @generatedvalue @column(name = "project_id", unique = true) private int id; private string name; private double budget; @onetoone(cascade = {cascadetype.all}) @joincolumn(name="consultantleader_id", updatable = false, insertable = false) private consultantleader consultantleader; @onetoone(cascade = {cascadetype.all}) @joincolumn(name="developerleader_id", updatable = false, insertable = false) private developerleader developerleader; @onetoone(cascade = {cascadetype.all}) @joincolumn(name="projectleader_id", updatable = false, insertable = false) private projectleader projectleader; public project() { } }
the problem is, i'm trying reference leader
types in project
class, when running main class, every time i've tried add project db, got following error:
java.lang.illegalstateexception: org.hibernate.transientpropertyvalueexception: object references unsaved transient instance - save transient instance beforequery flushing org.hibernate.transientpropertyvalueexception: object references unsaved transient instance - save transient instance beforequery flushing
that's why i've added cascade = {cascadetype.all}
@onetoone
annotation. problem now, annotation, inserts leaders
every time again in database, when i'm inserting new project, furthermore leaders
not in database in right columns in project
row (they empty).. how can solve this?
Comments
Post a Comment