Announcement Announcement Module
Collapse
No announcement yet.
Hibernate @ManyToMany problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate @ManyToMany problem

    Hello.
    I have 3 tables in DB. 2 for entities and one for the relations between them. I have 2 java classes:
    Code:
    	private static final long serialVersionUID = 1L;
    
    	@Id
    	@SequenceGenerator(name = "treatment_code_id_seq", sequenceName = "treatment_code_id_seq", allocationSize = 1)
    	private long id;
    
    	@Column(name = "code")
    	private String code;
    
    	@Column(name = "description")
    	private String description;
    
    
    	@ManyToMany(cascade = CascadeType.ALL)
    	@JoinTable(name = "category_code_relation", joinColumns = @JoinColumn(name = "code_id"), inverseJoinColumns = @JoinColumn(name = "category_id"))
    	private List<CPTCodeCategory> categories;
    ...
    WHen I try to call a controller method:
    Code:
    treatmentCode.getCategories()
    I receive the hibernate exception:
    Code:
    ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: org.mbs.billing.model.cpt.TreatmentCode.types, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.mbs.billing.model.cpt.TreatmentCode.types, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    	at org.hibernate.collection.PersistentBag.toString(PersistentBag.java:483)
    	at java.lang.String.valueOf(String.java:2826)
    	at java.io.PrintStream.println(PrintStream.java:771)
    	at org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:269)
    	at org.mbs.billing.controller.roadmap.impl.PhaseCompositionHelperImpl.getCategoryList(PhaseCompositionHelperImpl.java:307)
    	...
    As I understand the problem with initialization the list. What may be wrong here?

    Thank you in advance.

  • #2
    I have exactly the same problem.
    Hibernate session is closed when accessing the collection. The workaround is to set
    Code:
    @ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    but this is not a solution.
    I encountered the same problem when managing Hibernate by myself, I thought SpringRoo managed these issues by himself.

    Is there a solution?

    PS: I'm using Roo 1.1.5, STS 2.8.1

    Thanks,
    Stefano

    Comment

    Working...
    X