hibernate - JPA Mapping of Project Entity with Employee Entity -
i know how can map following requirements using jpa
i have project entity following attributes
projectno (auto generated) projectrequestedby approvedby
employee entity
employeenumber (primary key) employeename employeetitle employeephone
i know how can efficiently map employeenumber
projectrequestedby , approvedby attributes in project entity?
and know how possible display employeename
, employeetitle
, employeephone
attributes when execute query against project
entity class? ideally want have following record if try fetch project record
projectno projectrequestedbyname projectrequestedbytitle approvedbyname approvedbytitle
any highly appreciable.
update 1
project entity
@entity @table(name = "project") public class project private long projectno; @manytoone @joincolumn(name="emp_number", insertable =false, updatable=false) private employees employees;
employees entity
@entity @table(name = "employees") public class employees { @onetomany @joincolumn(name="emp_number", nullable = false) private collection<project> project;
ok. first of all, mapping wrong. in bidirectional onetomany association, 1 side must inverse side, , materialized mappedby
attribute. shown in documentation
you have 2 onetomany/manytoone betwen employee , project. employee requests many projects. an employee approves many projects.
so mapping should like
@entity public class employee @onetomany(mappedby = "requester") private set<project> requestedprojects; @onetomany(mappedby = "approver") private set<project> approvedprojects; } @entity public class project @manytoone private employee requester; @manytoone private employee approver; }
to project requester , approver in single query,
select p project p left join fetch p.requester left join fetch p.approver
Comments
Post a Comment