java - Hibernate - @SecondaryTable instead of @ManyToOne -
assume have table/class employee
:
@entity @table(name = "employee") public class employee { @id private string id; private string departmentid; ... public string getid() {return id;} public void setid(string id) {this.id = id;} public string getdepartmentid() {return departmentid;} public void setdepartmentid(string departmentid) {this.departmentid = departmentid;} ... }
and table/class department
:
@entity @table(name = "department") public class department { @id private string id; private string name; ... public string getid() {return id;} public void setid(string id) {this.id = id;} public string getname() {return name;} public void setname(string name) {this.name = name;} ... }
each employee belongs single department, , has single department id foreign key department's id primary key.
now assume i'd start retrieving employee's department name every time retrieve employee.
i could add code employee
:
@manytoone(fetch = fetchtype.eager) @joincolumn(name = "departmentid") private department department; public void setdepartment(department department) {this.department = department;} public string getdepartmentname() {return department.getname();}
(as employee
object being automatically converted json based on getters, , don't need rest of department
object in json, omit getter department
, add getter department.getname()
ensure that is included in json.)
however, means retrieving entire department
object - along many sub-objects , sub-sub-objects - every time want employee
, inefficient, database-wise.
i suppose start messing fetchtype
of of department
's sub-objects, need go , mess places use department
, rely on sub-objects, creating high risk of regression.
what like map department
secondarytable
of employee
, , map departmentname
property of employee
comes secondarytable
. i've used secondarytable
one-to-one mappings, , i've seen answers on site cannot used in place of one-to-many mappings, can't find simple information case - using in place of many-to-one mapping, think should have no problem working, conceptually. , yet can't seem right secondarytable
properties correctly set mapping.
is possible?
Comments
Post a Comment