TLDR:我在我的项目中使用Spring-jpa
和Hibernate
,我的问题发生在我尝试获取定义为{{的对象列表之后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() {
}
}
属性无效
答案 0 :(得分:2)