集合映射

发布于 2024-10-31 21:15:16 字数 255 浏览 0 评论 0原文

假设我有以下类:

class A1 {
  List<B1> bList;
}

class B1 {
  Long id;
}

---

class A2 {
  List<Long> bList;
}

我想使用 Dozer 将类 A1 映射到 A2,其中 A1.bList 包含 B1 对象,A2.bList 仅包含 B1 对象的 ID。

映射会是什么样子?

谢谢。

Suppose I have the following classes:

class A1 {
  List<B1> bList;
}

class B1 {
  Long id;
}

---

class A2 {
  List<Long> bList;
}

I want to map class A1 to A2 with Dozer, where A1.bList contains B1 objects and A2.bList contains only the IDs of the B1 objects.

How would the mapping look like?

Thank you.

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

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

发布评论

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

评论(3

勿挽旧人 2024-11-07 21:15:16

我认为您可以尝试设置 LongB1 的映射。如果我没记错的话,这只能以一种方式起作用,但我不记得是哪种方式。抱歉,希望这有帮助。

I think you could try setting up a mapping for Long to B1. If I remember rightly this only works one way, I can't remember which way tho. Sorry, hope this helps.

温柔嚣张 2024-11-07 21:15:16

您可以使用推土机定制转换器。 Dozer 客户转换器

:(可能有错误,没有编译或测试)

<mapping>
  <class-a>A1</class-a>
  <class-b>A2</class-b>    
  <field custom-converter="converters.YourCustomConverter">
    <a>bList</a>
    <b>bList</b>
  </field>
</mapping>

示例 自定义转换器:

public class YourCustomConverter implements CustomConverter {

    public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
        if (source == null) {
            return null;
        }
        if (source instanceof List<?>) {
            List<?> list = ((List<?>) source);
            if (list.isEmpty()) {
                return null;
            }
            if (list.get(0) instanceof B1) {
                List<Long> longList = new ArrayList<Long>();
                for (B1 b1 : list) {
                    longList.add(b1.getId());
                }
                return longList;
            } else (list.get(0) instanceof Long) {
                // do the inverse of the above
            } else {
                throw new MappingException("Wrong type ...");
            }
        } else {
            throw new MappingException("Converter YourCustomConverter used incorrectly. Arguments passed in were:"
                    + destination + " and " + source);
        }
    } 
}

You can use a dozer custom converter. Dozer customer converter

An example: (possible errors, didn't compile or test it)

<mapping>
  <class-a>A1</class-a>
  <class-b>A2</class-b>    
  <field custom-converter="converters.YourCustomConverter">
    <a>bList</a>
    <b>bList</b>
  </field>
</mapping>

The custom converter:

public class YourCustomConverter implements CustomConverter {

    public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
        if (source == null) {
            return null;
        }
        if (source instanceof List<?>) {
            List<?> list = ((List<?>) source);
            if (list.isEmpty()) {
                return null;
            }
            if (list.get(0) instanceof B1) {
                List<Long> longList = new ArrayList<Long>();
                for (B1 b1 : list) {
                    longList.add(b1.getId());
                }
                return longList;
            } else (list.get(0) instanceof Long) {
                // do the inverse of the above
            } else {
                throw new MappingException("Wrong type ...");
            }
        } else {
            throw new MappingException("Converter YourCustomConverter used incorrectly. Arguments passed in were:"
                    + destination + " and " + source);
        }
    } 
}
溺ぐ爱和你が 2024-11-07 21:15:16

我认为你可以通过覆盖 B1 中的 toString() 方法来做到这一点,它会起作用。

下面是示例代码:

@Override
public String toString() {
return new String(this.id);
}

在您的映射中进行以下更改:

<field>
 <a>bList</a>
 <b>bList</b>
 <a-hint>B</a-hint>
 <b-hint>java.lang.Long<b-hint>
</field>  

因此,当 dozer 尝试绑定 B1 时,它将返回其 id 作为 String,然后 dozer 将在 String 和 Long 之间执行自动转换。

I think you can do this by overridding toString() method in B1 and it would work.

Here's sample code:

@Override
public String toString() {
return new String(this.id);
}

And in your mapping do the following changes:

<field>
 <a>bList</a>
 <b>bList</b>
 <a-hint>B</a-hint>
 <b-hint>java.lang.Long<b-hint>
</field>  

So,when dozer tries to bind B1, it will return its id as String and then dozer will perform an automatic conversion between String and Long.

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