是否可以在针对目标 bean 中字符串类型的 bean 属性设置字符串值之前对其进行修剪?
例如,Dozer 通过其映射配置提供了这样的功能,
<configuration>
<trim-strings>true</trim-strings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
另请参阅推土机全局配置
使用 MapStruct 1.0.0.Final 我可以通过Expressions 或Before/After Mapping 自定义来实现这一点。
但是想知道是否有更好的方法来处理此类用例。
提前致谢。
有两个源类 A 和 B
class A {
public Double x;
public Double y;
}
class B {
public Double x;
public Double y;
}
Run Code Online (Sandbox Code Playgroud)
和另一个目标类 C
class C {
public Double x;
public Double y;
}
Run Code Online (Sandbox Code Playgroud)
很清楚如何将 A 映射到 C 或 B 到 C。
是否可以将某些功能(例如,源对象的添加或 pow)映射到目标对象,以便生成的代码如下所示
C.x = A.x + B.x
C.y = A.y + B.y
Run Code Online (Sandbox Code Playgroud)
或者
C.x = Math.pow(A.x, B.x)
C.y = Math.pow(A.y, B.y)
Run Code Online (Sandbox Code Playgroud) 我有这样的对象:
class User {
private String firstName;
private String secondName;
private int age;
....
get/set methods
}
Run Code Online (Sandbox Code Playgroud)
另一个对象将 User 作为属性:
class UserHolder {
private User user;
....
get/set methods
}
Run Code Online (Sandbox Code Playgroud)
我想将 UserHolder 转换为 User 使用MapStruct。
当我编写这个映射器时,我有这样的东西:
@Mapper(componentModel = "spring")
public interface UserHolderMapper {
@Mapping(source = "user.firstName", target = "firstName")
@Mapping(source = "user.secondName", target = "secondName")
@Mapping(source = "user.age", target = "age")
User toUser(UserHolder source);
}
Run Code Online (Sandbox Code Playgroud)
但它包含很多样板代码(在@Mapping注释中),我如何对映射器说我想将source.user映射到这个目标而不指定目标的字段?
我对这些技术很陌生,所以提前道歉。
我在我的应用程序中使用 springboot、Spring JPA 、 hibernate 和 mapstruct 。
我已插入 id 为“1”的父记录。现在,当我尝试为父级插入一个子级时,它会抛出
org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientPropertyValueException:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:嵌套异常为 java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:对象引用未保存的瞬态实例 -在刷新之前保存瞬态实例:
那么我如何指示mapstruct生成代码,以便它获取父记录,插入创建一个新对象,我猜这是导致上述问题的原因。
/**
* Mapper class for the entity {@link Child} and its corresponding data transfer object {@link ParentTlDTO}.
*/
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN, uses = {ParentMapper.class})
public interface ChildMapper extends EntityMapper<ChildDTO, Child> {
@Mapping(source = "parent.id", target = "parentId")
@Override
ChildDTO toDto(Child child);
@Mapping(source = "parentId", target = "parent.id")
@Override
Child toEntity(ChildDTO childDTO);
default Child fromId(String id) {
if (id == …Run Code Online (Sandbox Code Playgroud) 我如何使用 aHashMap<String, Object>作为对象的源?
这是我的目标对象:
public class ComponentStyleDTO{
private String attribute;
private Object value;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用我发现的这种方法,这也在文档中,但对我来说失败了。
我的映射器:
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = "spring", uses = ComponentStyleMapperUtil.class)
public interface ComponentStyleMapper {
ComponentStyleMapper MAPPER = Mappers.getMapper(ComponentStyleMapper.class);
@Mappings({@Mapping(target = "attribute", qualifiedBy = ComponentStyleMapperUtil.Attribute.class),
@Mapping(target = "value", qualifiedBy = ComponentStyleMapperUtil.Value.class)})
ComponentStyleDTO hashMapToComponentStyleDTO(HashMap<String, Object> hashMap);
}
Run Code Online (Sandbox Code Playgroud)
我的实用程序:
public class ComponentStyleMapperUtil{
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Attribute {
}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Value {
}
@Attribute
public String attribute(HashMap<String, Object> in){ …Run Code Online (Sandbox Code Playgroud) 我需要一些帮助来使用 MapStruct 映射嵌套的 POJO。我需要跳过/忽略目标类中所有子类的特定字段。
例如,以下是我的目标 POJO
class FullCustomerInfo {
UnitIdInfo unitIdInfo;
CustomerIdInfo customerIdInfo;
NameInfo nameInfo;
CustomerTypeInfo customerTypeInfo;
AddressInfo addressInfo;
}
Run Code Online (Sandbox Code Playgroud)
所有这些子类 - UnitIdInfo, CustomerIdInfo, NameInfo... 都包含一个字段“ fieldToIgnore”,我想在映射时忽略它。结构是这样的,这些类不共享包含此字段的公共基类 - “ fieldToIgnore”。
我创建的映射器如下所示:
@Mapper
public interface MapFullCustomer {
@Mappings({
@Mapping(target = "unitIdInfo.fieldToIgnore", ignore = true),
@Mapping(target = "customerIdInfo.fieldToIgnore", ignore = true),
@Mapping(target = "nameInfo.fieldToIgnore", ignore = true),
@Mapping(target = "customerTypeInfo.fieldToIgnore", ignore = true),
@Mapping(target = "addressInfo.fieldToIgnore", ignore = true)
})
public FullCustomerInfo map(SourceCustomerInfo sourceCustomerInfo);
}
Run Code Online (Sandbox Code Playgroud)
这有效。
但是,当子类的 …
解决方案:
我不得不更改 mymapstruct和lombok annotationProcessorPaths.
我不得不放在mapstruct上面lombok,然后它起作用了。
我将下面的 pom 更新为工作版本,所以这里没有非工作代码。
我还将 lombok 版本转换回当前版本,而不是使用边缘版本。
原问题:
我有 2 组或多或少相同的类(见下面的例子)
最初我将项目设置为使用:
我发现 Lombok 文档解释了如何将注释处理器添加到 maven-plugin https://projectlombok.org/setup/maven
但是在执行时我仍然得到 Error:(16,25) java: ClassX does not have an accessible parameterless constructor.
搜索此消息时,我发现了一些 2 到 3 年的问题,但没有最新的。我也看到,这些帖子的问题已经解决。
至少在其中一篇文章中提到,当将项目拆分为模块时,它起作用了。这对我也有效。当我将 DTO 移动到另一个 maven 模块时,在那里构建它们并设置它工作的依赖项,但这绝对不是我想要的项目结构。此外,因为我可能还需要将我的实体移出,并且我不想为我正在创建的每个 Pojo 结构创建一个新模块。
我还发现 Lombok Edge 版本上的帖子:https : //projectlombok.org/download-edge 更改列表中的第二点是 …
在mvn clean install使用 Java 11 在 IntelliJ Idea 中智能执行时,我收到以下警告:
警告:java:来自注释处理器 'net.java.dev.hickory.prism.internal.PrismGenerator' 的支持的源版本 'RELEASE_6' 小于 -source '11'
如何修复它以及它来自哪里?注意我使用:
1.18.121.3.1.Final我还使用默认设置在 IDE 中启用了注释处理:
Build, Execution, Deployment-> Compiler-> Annotation processors-> 检查Enable annotation processing我可以在有字符串到枚举映射的地方找到答案,但我找不到如何将枚举映射到字符串。
public class Result {
Value enumValue;
}
public enum Value {
TEST,
NO TEST
}
public class Person {
String value;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能映射这个?
我试过 :
@Mapping(target = "value", source = "enumValue", qualifiedByName = "mapValue")
@Named("mapValue")
default Person mapValue(final Value en) {
return Person.builder().value(en.name()).build();
}
Run Code Online (Sandbox Code Playgroud) 通过双向一对多关联执行从数据传输对象 (DTO) 到Hibernate实体的MapStruct映射的最佳方法是什么?
假设我们有一个与它链接的BookDto类型的多个评论:ReviewDto
public class BookDto {
private List<ReviewDto> reviews;
// getter and setters...
}
Run Code Online (Sandbox Code Playgroud)
相应的 Hibernate 实体与以下对象Book具有一对多关联Review:
@Entity
public class Book {
@OneToMany(mappedBy = "book", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Review> reviews = new ArrayList<>();
public void addReview(Review review) {
this.reviews.add(review);
review.setBook(this);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Review {
@ManyToOne(fetch = FetchType.LAZY)
private Book book;
public void setBook(Book book) {
this.book = book;
}
//... …Run Code Online (Sandbox Code Playgroud)