Re: Inheritance Mapping
Armin Waibel <[email protected]>
| Newsgroups | gmane.comp.jakarta.ojb.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Adam,
Adam Brons wrote:
> Hey Everyone,
>
> I have a question about mapping inheritance.
>
> What I'm having trouble doing is reading and updating a collection of a
> subclasses. It writes the Manager entry with the Manager => Employee
> mapping without problem. Unfortunately when I read in the Company class
> and it loads a collection of employees it does not collect the Manager =>
> Employee mapping. Based on the method of Inheritance I used I was under
> the impression that OJB would use the class listed in ojbConcreteClass
> (class_name) as the class to load the data into, and since that class in
> some cases is a Manager, it should load and populate the Manager's
> Collection of Employees.
>
> Any help you can offer would be most appreciated. Thank!
>
I think you mixed up two different inheritance strategies.
table per class
file:///E:/dev/db-ojb-1b/ojb/doc/docu/guides/advanced-technique.html#Mapping+Each+Class+of+a+Hierarchy+to+a+Distinct+Table+%28table+per+class%29
table per subclass
file:///E:/dev/db-ojb-1b/ojb/doc/docu/guides/advanced-technique.html#Mapping+Each+Subclass+to+a+Distinct+Table+%28table+per+subclass%29
You use a discriminator column to identify the real class (used in
"table per class"), but in your posted database table script it seems
you expect a "table per subclass" mapping.
Could this be the issue?
regards,
Armin
>
> The following is an EXAMPLE of what I'm trying to do.
>
> Classes --- Based on documentation examples:
> ==================================
> public class Company {
> private Integer id;
> private Collection<Employee> employees;
> private String name;
>
> public Company() { }
>
> public Integer getId() {
> return id;
> }
> public void setId(Integer id) {
> this.id = id;
> }
> public Collection<Employee> getEmployees() {
> if (employees == null)
> employees = new ArrayList();
>
> return employees;
> }
> public void setEmployees(Collection<Employee> employees) {
> this.employees = employees;
> }
> public String getName() {
> return name;
> }
> public void setName(String name) {
> this.name = name;
> }
> }
>
> public class Employee {
> protected Integer id;
> protected Integer companyId;
> protected String name;
> protected String ojbConcreteClass;
>
> public Employee() { this.ojbConcreteClass =
> this.getClass().getName(); }
>
> public Integer getId() {
> return id;
> }
> public void setId(Integer id) {
> this.id = id;
> }
> public Integer getCompanyId() {
> return companyId;
> }
> public void setCompanyId(Integer companyId) {
> this.companyId = companyId;
> }
> public String getName() {
> return name;
> }
> public void setName(String name) {
> this.name = name;
> }
> public String getOjbConcreteClass() {
> return ojbConcreteClass;
> }
> public void setOjbConcreteClass(String ojbConcreteClass) {
> this.ojbConcreteClass = ojbConcreteClass;
> }
> }
>
> public class Manager extends Employee {
> private Collection<Employee> employees;
>
> public Manager() { super(); }
>
> public Collection<Employee> getEmployees() {
> if (employees == null)
> employees = new ArrayList();
>
> return employees;
> }
> public void setEmployees(Collection<Employee> employees) {
> this.employees = employees;
> }
> }
>
> Database(DB2) tables:
> ================
> CREATE TABLE company (
> company_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1
> INCREMENT BY 1),
> name varchar(255) NOT NULL,
> --
> PRIMARY KEY (company_id)
> );
>
> CREATE TABLE employee (
> employee_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1
> INCREMENT BY 1),
> name varchar(255) NOT NULL,
> company_id_FK INTEGER NOT NULL,
> class_name varchar(255) NOT NULL,
> --
> PRIMARY KEY (employee_id),
> FOREIGN KEY (company_id_FK) REFERENCES company (company_id)
> );
>
> CREATE TABLE manager_employee_map (
> manager_id_FK INTEGER NOT NULL,
> employee_id_FK INTEGER NOT NULL,
> --
> FOREIGN KEY (manager_id_FK) REFERENCES employee (employee_id),
> FOREIGN KEY (employee_id_FK) REFERENCES employee (employee_id)
> );
>
> Repository mapping:
> ===============
> <class-descriptor class="Company" table="COMPANY">
> <field-descriptor name="id" column="COMPANY_ID"
> jdbc-type="INTEGER"
> primarykey="true" autoincrement="true" access="readonly"/>
> <field-descriptor name="name" column="NAME" jdbc-type="VARCHAR" />
>
> <collection-descriptor name="employees"
> element-class-ref="Employee"
> auto-delete="true" auto-update="true" auto-insert="true"
> auto-retrieve="true">
> <inverse-foreignkey field-ref="CompanyId"/>
> </collection-descriptor>
> </class-descriptor>
>
> <class-descriptor class="Employee" table="employee">
> <extent-class class-ref="Manager"/>
> <field-descriptor name="id" column="EMPLOYEE_ID"
> jdbc-type="INTEGER"
> primarykey="true" autoincrement="true" access="readonly"/>
> <field-descriptor name="name" column="NAME" jdbc-type="VARCHAR" />
> <field-descriptor name="companyId" column="COMPANY_ID_FK"
> jdbc-type="INTEGER"/>
> <field-descriptor name="ojbConcreteClass" column="CLASS_NAME"
> jdbc-type="VARCHAR" />
> </class-descriptor>
>
> <class-descriptor class="Manager" table="employee">
> <field-descriptor name="id" column="EMPLOYEE_ID"
> jdbc-type="INTEGER"
> primarykey="true" autoincrement="true" access="readonly"/>
> <field-descriptor name="name" column="NAME" jdbc-type="VARCHAR" />
> <field-descriptor name="companyId" column="COMPANY_ID_FK"
> jdbc-type="INTEGER"/>
> <field-descriptor name="ojbConcreteClass" column="CLASS_NAME"
> jdbc-type="VARCHAR" />
>
> <collection-descriptor name="employees"
> element-class-ref="Employee"
> auto-retrieve="true" auto-update="true" auto-delete="true"
> indirection-table="MANAGER_EMPLOYEE_MAP">
> <fk-pointing-to-this-class column="MANAGER_ID_FK"/>
> <fk-pointing-to-element-class column="EMPLOYEE_ID_FK"/>
> </collection-descriptor>
> </class-descriptor>
>
>
> Adam Brons