使用序列化进行深度克隆后,hibernate延迟加载异常

问题描述 投票:3回答:1

TLDR:我在我的项目中使用Spring-jpaHibernate,我的问题发生在我试图获得在我使用Lazy方法深度克隆的实体中定义为org.apache.commons.lang3.SerializationUtils.clone()的对象列表之后。

我得到以下异常

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)
    at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
    at JSON_to_XML.main(JSON_to_XML.java:84)

在我试图获得在users对象中定义为lazy的所有duplicateEvent的列表之后

代码 :

@Service
public class EventService {

@Autowired
EventRepository eventRepository;

 public List<User> duplicateEvent(Long id) {
    Event event =  eventRepository.findById(id);
    Event duplicateEvent = SerializationUtils.clone(event);

    return duplicateEvent.getUsers();
  }
}

活动实体:

@Entity
@Table(name="events")
public class Event implements Serializable{

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

 @OneToMany(fetch= FetchType.LAZY, mappedBy="event")
 privare List<User> users = new ArrayList();

  public List<User> getUsers() {
    return this.users;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Event() {

  }
}

p.s:spring.jpa.hibernate.enable_lazy_load_no_trans=true财产没有帮助

java spring hibernate spring-data spring-data-jpa
1个回答
2
投票

自己搞清楚了

而不是与org.apache.commons.lang3.SerializationUtils.clone()深刻复制

我使用Dozer Mapper进行深层复制,并且不再有例外。

© www.soinside.com 2019 - 2024. All rights reserved.