gpt4 book ai didi

java - Hibernate + Dozer - 在映射的集合项属性上使用 map-id

转载 作者:行者123 更新时间:2023-12-01 07:57:06 24 4
gpt4 key购买 nike

我有两个 hibernate 实体:

@Entity
@Table(name = DBConstants.TABLE_PARTNER)
public class Partner extends XWeedEntity {

private static final long serialVersionUID = 5692151244956513381L;

@Id
@Column(name = DBConstants.PARTNER_COL_PARTNER_NUMBER)
private Integer partnerNumber;

@OneToMany(mappedBy = DBConstants.VISIT_PROP_VISITOR)
private List<Visit> visits;

// More fields and properties...
}


@Entity
@Table(name = DBConstants.TABLE_VISIT)
public class Visit extends XWeedEntity {

private static final long serialVersionUID = -8324746049334117579L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = DBConstants.VISIT_COL_ID)
private Integer id;

@ManyToOne
@JoinColumn(name = DBConstants.VISIT_COL_VISITOR, nullable = false)
private Partner visitor;

// More fields and properties...
}

还有两个 DTO 实体:

public class PartnerDto extends XWeedEntity {

private static final long serialVersionUID = 5692151244956513381L;
private Integer partnerNumber;
private List<VisitDto> visits;

// More fields and properties...
}


public class VisitDto extends XWeedEntity {

private static final long serialVersionUID = -8324746049334117579L;
private Integer id;
private PartnerDto visitor;

// More fields and properties...
}

我有以下推土机映射:

<mapping map-id="partnerWithCollections" map-empty-string="false" map-null="false">
<class-a>org.xweed.model.app.domain.dbo.Partner</class-a>
<class-b>org.xweed.model.app.domain.dto.PartnerDto</class-b>

<field map-id="visitWithPartner">
<a>visits</a>
<b>visits</b>
</field>
</mapping>

<mapping map-id="partnerBasic" wildcard="false" map-empty-string="false" map-null="false">
<class-a>org.xweed.model.app.domain.dbo.Partner</class-a>
<class-b>org.xweed.model.app.domain.dto.PartnerDto</class-b>

<field>
<a>partnerNumber</a>
<b>partnerNumber</b>
</field>
</mapping>

<mapping map-id="visitWithPartner" map-empty-string="false" map-null="false">
<class-a>org.xweed.model.app.domain.dbo.Visit</class-a>
<class-b>org.xweed.model.app.domain.dto.VisitDto</class-b>

<field map-id="partnerBasic">
<a>visitor</a>
<b>visitor</b>
</field>
</mapping>

问题是,当我使用“partnerWithCollections”映射调用推土机时,推土机正在映射来自合作伙伴访问的所有访问对象,但是每次访问都有它的访问者及其访问集合等等,当每次访问的访问者属性应该只包含 partnerNumber。

如果我尝试从访问中排除访问者字段,那么会起作用,并且每个访问者的访问都是空的,但由于某种原因,它无法使用 map-id 来使用一些具体的合作伙伴映射。

有什么想法吗?

提前致谢。

最佳答案

发生这种情况是因为默认情况下,dozer 总是映射具有相同名称的属性:

  • 合作伙伴访问次数
  • PartnerDto.visits

您可以通过 属性“wildcard=false”禁用此设置。

来自官方文档:

Does Dozer automatically map fields with matching property names?

Yes. All fields with matching property names are implicitly mapped. It would be atypical usage, but you could suppress this behavior by setting wilcard="false".

http://dozer.sourceforge.net/documentation/faq.html#auto-property-name

您也可以使用全局配置对所有映射执行此操作:

<configuration>
<wildcard>false</wildcard>
</configuration>

http://dozer.sourceforge.net/documentation/globalConfiguration.html

关于java - Hibernate + Dozer - 在映射的集合项属性上使用 map-id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26010469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com