Dalen
Dalen

Reputation: 8986

Dozer can't map Hibernate's persistentBag to List

I'm implementing a rest web service in Java as university assignment so I'm quite new to such things thus probably I'm doing it in the wrong way.

Anyway the problem is that Dozer can't map a Hibernate PersistentBag to a java.util.List.

I'm mapping two classes: A and B,

List<Department> A.departments

is mapped to

DepartmentsType B.departments

DepartmentsType has just one property which is

List<DepartmentType> departments

So in the end A.departments is mapped to B.departments.departments, infact this is the mapping:

<mapping>
    <class-a>it.unitn.lsde.persistence.A</class-a>
    <class-b>it.unitn.lsde.generated.B</class-b>

    <field>
        <a>name</a>
        <b>name</b>
    </field>
    <field>
        <a>city</a>
        <b>city</b>
    </field>
    <field> 
        <a>departments</a>
        <b>departments.departments</b>
        <a-hint>it.unitn.lsde.persistence.Department</a-hint>
        <b-hint>it.unitn.lsde.generated.DepartmentType</b-hint>
    </field>
    <field>
        <a>code</a>
        <b>code</b>
    </field>
</mapping>

This is how I define A.departments (Hibernate):

@OneToMany(fetch = FetchType.LAZY, mappedBy = "university", cascade = CascadeType.ALL)
protected List<Department> departments = new ArrayList<Department>();

This is how I define B.departments:

DepartmentsType departments;

And eventually this is how DepartmentsType.departmens is defined:

List<DepartmentType> departments;

Whenever a I try to map class A to class B I get a Field Mapping Error from Dozer:

Source parent class: B
Source field name: departments
Source field type: class org.hibernate.collection.PersistentBag
Source field value: [it.unitn.lsde.persistence.Department@12b3c6b]
Dest parent class: A
Dest field name: departments.department
Dest field type: java.util.List

The exceptions stack point this code to be problemitic:

return (B) mapper.map((A)session.get(A.class, id), B.class);

However when I map class B to class A everything works as expected. I know that, for a number of reasons, Hibernate is turning what I defined as List into its PersistentBag but this should be a List itself.

I'm not able to find out what i'm doing wrong, why Dozer can map A to B but not B to A.

Thanks in advance

Upvotes: 2

Views: 4976

Answers (1)

Dalen
Dalen

Reputation: 8986

After hours of searching I've found out that I was missing the setter of the List on the DepartmentsType's (B.departments) class.

Without that setter Dozer was not able to map the list coming from Hibernate.

Upvotes: 1

Related Questions