【发布时间】:2022-02-08 02:36:10
【问题描述】:
我有 2 个实体,具有一对一关联(ProfileEntity 和 VCardEntity)
实体电子名片:
@Entity
@Table(name = "vcard")
@AllArgsConstructor
@NoArgsConstructor
@Data
@SequenceGenerator(name="vcard_id_seq_generator", sequenceName="vcard_id_seq", allocationSize = 1)
public class VCardEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "vcard_id_seq_generator")
@Column(name="vcard_id")
Long id;
String account;
@Column(name = "first_name")
String firstName;
@Column(name = "last_name")
String lastName;
@Column(name = "pbxinfo_json")
String pbxInfoJson;
@Column(name = "avatar_id")
String avatarId;
@OneToOne(mappedBy = "vcard")
ProfileEntity profile;
}
实体简介:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "profile")
public class ProfileEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "profile_id")
private Long profileId;
private String account;
@Column(name = "product_id")
private String productId;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vcard_id", referencedColumnName = "vcard_id")
private VCardEntity vcard;
}
我使用 map 结构如下:
public class CycleAvoidingMappingContext {
private Map<Object, Object> knownInstances = new IdentityHashMap<Object, Object>();
@BeforeMapping
public <T> T getMappedInstance(Object source, @TargetType Class<T> targetType) {
return targetType.cast(knownInstances.get(source));
}
@BeforeMapping
public void storeMappedInstance(Object source, @MappingTarget Object target) {
knownInstances.put( source, target );
}
}
@Mapper(componentModel = "spring")
public interface EntityToProfile {
ProfileEntity profileToEntity(Profile profile, @Context CycleAvoidingMappingContext context);
Profile entityToProfile(ProfileEntity entity, @Context CycleAvoidingMappingContext context);
}
@Mapper(componentModel = "spring")
public interface EntityToVCard {
VCard entityToVcard(VCardEntity entity, @Context CycleAvoidingMappingContext context);
VCardEntity vcardToEntity(VCard vcard, @Context CycleAvoidingMappingContext context);
}
最后我在我的服务中调用映射:
@Service
@RequiredArgsConstructor
@Slf4j
public class DefaultChatService implements ChatService {
private final ProfileRepository profileRepository;
private final EntityToProfile entityToProfileMapper;
private final EntityToVCard entityToVCardMapper;
@Override
public List<Profile> findAllProfile(Optional<Long> id) {
if (id.isPresent()) {
Optional<ProfileEntity> result = profileRepository.findById(id.get());
if (result.isPresent()) {
Profile profile = entityToProfileMapper.entityToProfile(result.get(), new CycleAvoidingMappingContext());
return Stream.of(profile).collect(Collectors.toList());
}
}
return new ArrayList<Profile>();
}
}
我得到了错误 ERROR 15976 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]:Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [Handler发送失败;嵌套异常是 java.lang.StackOverflowError] 的根本原因 java.lang.StackOverflowError: null
有什么想法可以解决吗? 在我看来,我所做的一切都是在这里写的 Prevent Cyclic references when converting with MapStruct 但它对我不起作用
【问题讨论】: