我在mapstruct映射器上遇到了问题。当运行mvn clean install(或mvn clean compile)时,我会得到以下错误:
[ERROR] /mapper/EntityMapper.java:[28,7] Can't map property "java.util.List<com.socomec.tseselector.model.Source> architecture.sources
" to "java.lang.Integer architecture.sources". Consider to declare/implement a mapping method: "java.lang.Integer map(java.util.List<com.socomec.tseselector.model.Source> value)".
[ERROR] /mapper/command/TSEProjectCommandMapper.java:[21,16] Can't map property "java.lang.Integer architecture.loads" to "java.util.List<com.socomec.tseselector.model.Load> architecture.loads". Consider to declare/implement a mapping method: "java.util.List<com.socomec.tseselector.model.Load> map(java.lang.Integer value)".
问题是我不知道mapstruct从哪里获得这个“java.lang.Integer Architecture.Loads”。我不明白这个整数是从哪里来的,正如您在我的代码中看到的,没有整数。而且,到目前为止,我在使用类似的映射器时从未遇到过这个错误。
下面是我的实体架构:
public class Architecture {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String imagePath;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="tse_architectures_loads",
joinColumns = {@JoinColumn(table = "tse_architecture", name = "architecture_id")},
inverseJoinColumns = {@JoinColumn(table = "tse_load", name = "load_id")}
)
private List<Load> loads;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="tse_architectures_sources",
joinColumns = {@JoinColumn(table = "tse_architecture", name = "architecture_id")},
inverseJoinColumns = {@JoinColumn(table = "tse_source", name = "source_id")}
)
private List<Source> sources;
private String techno;
public Architecture() {
this.loads = new ArrayList<>();
this.sources = new ArrayList<>();
}
DTO
public class ArchitectureDto {
private Long id;
private String name;
private String imagePath;
private List<LoadDto> loadDtos;
private List<SourceDto> sourceDtos;
private String techno;
public ArchitectureDto() {
this.loadDtos = new ArrayList<>();
this.sourceDtos = new ArrayList<>();
}
}
我的映射器:
@Mapper(componentModel = "spring")
public interface ArchitectureMapper extends EntityMapper<ArchitectureDto, Architecture> {
@Mapping(source = "loads", target = "loadDtos")
@Mapping(source = "sources", target = "sourceDtos")
ArchitectureDto toDto(Architecture architecture);
}
public interface EntityMapper<D, E> {
/**
* Map a DTO to an Entity
*
* @param dto the dto to map
* @return an Entity
*/
E toEntity(D dto);
/**
* Map an Entity to a DTO
*
* @param entity to map to a DTO
* @return a DTO
*/
D toDto(E entity);
/**
* Map a List of DTOs to a List of Entities
*
* @param dtoList the list to map
* @return a list of Entities
*/
List<E> toEntity(List<D> dtoList);
/**
* Map a list of Entities to a list of DTOs
*
* @param entityList the list to map
* @return a list of DTOs
*/
List<D> toDto(List<E> entityList);
}
public class Load {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private String unity;
private Long sourcePriority1;
private Long SourcePriority2;
private Long SourcePriority3;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
private Long iccValue;
@JsonIgnore
@ManyToMany(mappedBy = "loads")
private List<TSEProject> tseProjects;
@JsonIgnore
@ManyToMany(mappedBy = "loadList")
private List<Architecture> architectures;
public Load() {
this.architectures = new ArrayList<>();
this.tseProjects = new ArrayList<>();
}
public class Source {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private String unity;
private Long quantity;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
@JsonIgnore
@ManyToMany(mappedBy = "sources")
private List<TSEProject> tseProjects;
@JsonIgnore
@ManyToMany(mappedBy = "sourceList")
private List<Architecture> architectures;
public Source() {
this.architectures = new ArrayList<>();
this.tseProjects = new ArrayList<>();
}
}
@Data
public class LoadDto {
private Long id;
private String name;
private String type;
private String unity;
private Long sourcePriority1;
private Long SourcePriority2;
private Long SourcePriority3;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
private Long iccValue;
private List<Long> tseProjectsId;
public LoadDto() {
this.tseProjectsId = new ArrayList<>();
}
}
@Data
public class SourceDto {
private Long id;
private String name;
private String type;
private String unity;
private Long quantity;
private Long kvaValue;
private Long kwValue;
private Long pfValue;
private Long aValue;
}
我想你需要再排除一些故障。尝试帮助MapStruct并定义一个附加的映射方法,如下所示:
Mapper(componentModel = "spring")
public interface ArchitectureMapper extends EntityMapper<ArchitectureDto, Architecture> {
@Mapping(source = "loads", target = "loadDtos")
@Mapping(source = "sources", target = "sourceDtos")
ArchitectureDto toDto(Architecture architecture);
// first try with all properties ignored
// @Mapping( target = "id", ignore = true ) etc.
SourceDto toDto(Source source);
// first try with all properties ignored
// @Mapping( target = "id", ignore = true ) etc.
LoadDto toDto(Load load);
}
问题是MapStruct试图基于属性名自动映射这些对象。如果它在某个地方发现了相同的名称,它会尝试进行类型转换或使用现有的映射方法来完成它的工作。它甚至尝试了两个步骤(转换,然后是一个方法),这有时会导致这些结果。
您可以提供MapStruct试图自己生成的一些映射,然后再次忽略所有属性,如上面的示例所示。逐个删除忽略行,然后查看MapStruct何时开始抱怨。
像这样的调试有点乏味,但可能在对象图的深处发现了问题。然后,您可以再次删除所有签名,除了解决问题的签名,从而最大限度地利用代码生成。
在我的数据库表中,我有一个列,它的内容可以是Y或N(CHECK约束)。我在Java中定义了一个String属性,在我的类中定义了一个基于String的setter。此外,为了方便起见,我添加了一个带有布尔参数的setter/getter。 那么,Hibernate基于什么属性进行映射呢?方法名称?参数名称?参数类型?在setter中使用可以吗?如果我的属性的名称与表列的名称不同,这会有什么不同吗?
MapStrut的新成员;对象到字符串错误: [错误] /util/LicenseMapper.java:[11,23]无法映射属性" Java . lang . object license . custom fields[]。值" to " Java . lang . string license . custom fields[]。值”。考虑声明/实现一个映射方法:“Java . lang
我试图在我的Jasper报告中实现目录。以下是Jasper Reports网站上提供的一个如何实现该操作的示例:https://sourceforge.net/p/jasperReports/code/ci/jr-6-2-1/tree/jasperReports/demo/samples/tableofcontents/Reports/ 在上面的示例中,它们对数据库运行一个查询,以获取数据来填充报
我需要将源类中的字段值映射到字段属性。我可以使用Mapstruct使用@mapper注释的'expression'参数来完成 有没有其他方法可以不使用“表达式”来进行映射?
问题内容: 我将Spring JPA与Hibernate和Postgres一起使用 在实体中,我尝试使用List和integer [] 在数据库中,我有一列类型: 有什么JPA使用方式吗? 问题答案: JPA无法直接将数组持久化到单独的表或数据库数组(例如,映射到的数组)。因此,您有两种方法: 1)用于将该列另存为BLOB或CLOB 2)使用而不是数组
使用存储历史对象时区信息的系统,如下所示: 如果它能存储像“美国/纽约”这样的IANA时区,生活会轻松得多,但这就是我必须处理的问题。我的最终目标是将这些历史日期与当前时间进行比较,并获得它们之间的时差。 创建当前时间很容易。我可以用UTC得到它 或者我的本地时区 问题是,如何将表数据读入分区日期时间(ZonedDateTime)进行苹果对苹果的比较。比如一些神奇的方法,比如