Dozer 无法将 Hibernate 的 permanentBag 映射到 List

发布于 2024-12-14 21:09:26 字数 2583 浏览 1 评论 0原文

我正在用 Java 实现一个 REST Web 服务作为大学作业,所以我对这些事情很陌生,因此可能我做错了。

不管怎样,问题是 Dozer 无法将 Hibernate PersistentBag 映射到 java.util.List 。

我正在映射两个类:AB

List<Department> A.departments

映射到

DepartmentsType B.departments

DepartmentsType只有一个属性,所以

List<DepartmentType> departments

最后是A .departments 映射到 B.departments.departments,事实上这是映射:

<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>

这就是我定义 A.departments (Hibernate)的方式:

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

这就是如何我定义B.departments

DepartmentsType departments;

最终这就是 DepartmentsType.departmens 的定义方式:

List<DepartmentType> departments;

每当我尝试将 class A 映射到 class B 我从 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

异常堆栈指出此代码有问题:

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

但是,当我将 class B 映射到 class A< /code> 一切都按预期进行。 我知道,由于多种原因,Hibernate 正在将我定义的 List 转换为它的 PersistentBag ,但这应该是一个 List 本身。

我无法找出我做错了什么,为什么 Dozer 可以将 A 映射到 B 但不能将 B 映射到 一个。

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-12-21 21:09:26

经过几个小时的搜索,我发现我缺少 DepartmentsType 的 (B.departments) 类中的 List 的 setter。

如果没有这个 setter,Dozer 就无法映射来自 Hibernate 的列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文