failed to lazily initialize a collection and vaadin
failed to lazily initialize a collection and vaadin
Hello I have a problem with vaadin and lazy load of my entity
my entity contains a rellationship one-to-many and many-to-one with this way
@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()
so i have this controller
@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();
when i try to load and print for each entity her children it was successfull but it was on setup.When i use my form in vaadin and i call service.all()
and try to do the same thing i have the
org.hibernate.LazyInitializationException: failed to lazily
initialize a collection of role: models.Category.children, could not
initialize proxy - no Session
Service:
@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());
What can i do to have wanted result?
@kc007 nothing happend, i was wondering if there is any annotation for my view so it can keep the session alive
– zdratoz
Aug 17 at 4:26
There is proporty has to added in application.proporties as by default that true which do not load lazy, you have to make it false, I don’t remember that need to check that property name then it should work, let me check that.
– kc007
Aug 17 at 4:30
Can you please put method @transactional notation in service and check
– kc007
Aug 17 at 4:34
@kc007 already have one :/
– zdratoz
Aug 17 at 4:40
1 Answer
1
i solved my problem by this way
i initialized my data-providers in my enter method and i did this
@Override
public void enter(ViewChangeListener.ViewChangeEvent event)
parents = DataProvider.ofCollection(service.all());
parentGrid.setDataProvider(parents);
edit also i put my selectionListener into the enter method
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Remove Cascading from entity relationship cascade= CascadeType.ALL and check.
– kc007
Aug 17 at 4:15