【发布时间】:2019-01-24 01:51:22
【问题描述】:
您好,我的实体的 vaadin 和延迟加载有问题 我的实体通过这种方式包含一对多和多对一的关系
@Entity
@Table(name = "Categories")
public class Category extends AbstractEntity {
private static final long serialVersionUID = 1L;
private String name;
@ManyToOne
@JoinColumn(name = "parent")
private Category parent;
@Column
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
private List<Category> children = new ArrayList<>();
public Category() { }
所以我有这个控制器
@Transactional
public class CategoryService{
@PersistenceContext
private EntityManager em;
public void save(Category category) {
..
}
public Collection<Category> all() {
return em
.createQuery("select category from Category category",Category.class)
.getResultList();
}
当我尝试为每个实体加载和打印她的孩子时,它是成功的,但它是在设置中。当我在 vaadin 中使用我的表单并调用 service.all() 并尝试做同样的事情我有
org.hibernate.LazyInitializationException: 懒惰失败 初始化角色集合:models.Category.children,不能 初始化代理 - 没有会话
服务:
@Inject
public CategoriesView(CategoryService service) {
this.service = service;
list= (List<Category>) service.all();
}
@Override
protected Component initContent() {
HorizontalLayout layout = new HorizontalLayout();
for (Category cate : list) {
System.out.println(cate.getChildren());
}
}
我该怎么做才能得到想要的结果?
【问题讨论】:
-
从实体关系 cascade= {CascadeType.ALL} 中移除级联并检查。
-
@kc007 什么也没发生,我想知道是否有任何注释可以让我的视图保持活动状态
-
必须在 application.proporties 中添加比例,因为默认情况下 true 不会加载惰性,您必须将其设为 false,我不记得需要检查该属性名称然后它应该可以,让我检查一下。
-
您能否将@transactional 表示法投入使用并检查
-
@kc007 已经有一个:/
标签: java hibernate vaadin lazy-loading