TLDR:我在我的项目中使用Spring-jpa
和Hibernate
,我的问题发生在我试图获得在我使用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
财产没有帮助