将列表<b>对象分配到列表<a> object

时间:2017-08-08 22:19:36

标签: list hibernate maven mapping

I have a problem that I want to know if there is a magic trick or pattern to solve this in the other way.

I have list of B class objects, but I need to return list of A class objects. 'A' and 'B' classes have exactly the same fields (I need to return List, but I have List because it was mapped from database by hibernate) and I need to return it inside List of A objects because I can not import B class to other project (where class A is) beacuse of maven cyclic reference.

I just rewrote all fields from every object in for loop.

Is there any other way to solve this without doing it inside the loop?

Thanks in advance for discussion :) Cheers! :)

2 个答案:

答案 0 :(得分:0)

您是否尝试返回对象类型列表?然后可以将其类型转换为B类型。

答案 1 :(得分:0)

在一个完美的世界中,你会从一个基类扩展这两个类,使它们成为可以接受的,但我发现这在这里是不可能的。

在我看来,你的approch已经是解决问题的最佳方式了。

另一种方式是通过反思来解决它。这不是可以重复的 - 只是指出这一点。

在此解决方案中,您迭代给定对象的所有声明字段,读出值并将对象中具有相同名称的字段设置为此值。

像我说的那样,它不是可以推荐的,因为它依赖于可能会改变的字段名称。