使用序列化

时间:2017-12-27 09:28:15

标签: java spring hibernate spring-data spring-data-jpa

TLDR:我在我的项目中使用Spring-jpaHibernate,我的问题发生在我尝试获取定义为{{的对象列表之后1}}在我使用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();
  }
}

p.s:@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() { } } 属性无效

1 个答案:

答案 0 :(得分:2)

自己想出来

而非使用org.apache.commons.lang3.SerializationUtils.clone()

进行深层复制

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