I have a PapelEntity class that has many-to-many relationship with the CompetenciaEntity.
The problem can be seen in the method initialize collection is as initilized. And the log shows the collection as uninitialized. So, when the object gets in the client app, I get a LazyInitializationException
A very important thing, this functioned very well with hibernate 2.1.8
Somebody knows what it can be happening? To upgrade the version of hibernate 2,1,8 to 3.1 did I miss something?
Excuse my bad english :P
Hibernate version: 3.1 beta 3
Class and Mapping documents:
PapelEntity.class
Code:
/**
* @hibernate.class table="PAPEL"
* proxy="br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity"
*/
public class PapelEntity extends AbstractEntity {
private static final long serialVersionUID = -2155700128326009722L;
private String nome;
private String descricao;
private List<CompetenciaEntity> competencias;
/**
* @hibernate.id type = "int" column = "ID" unsaved-value = "-1" generator-class = "sequence"
* @hibernate.generator-param name = "sequence" value = "papel_id_seq"
*/
@Override
public int getId() {
return super.getId();
}
/**
* @hibernate.property type = "string"
* @hibernate.column length = "60" name = "NOME" not-null = "true" sql-type = "VARCHAR" unique = "true"
*/
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
/**
* @hibernate.property type = "string"
* @hibernate.column length = "200" name = "DESCRICAO" not-null = "false" sql-type = "VARCHAR" unique = "false"
*/
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
/**
* @hibernate.bag cascade="none"
* table="PAPELCOMPETENCIA"
* @hibernate.collection-many-to-many class="br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity"
* column="COMPETENCIA"
* @hibernate.collection-key column = "PAPEL"
*/
public List getCompetencias() {
return competencias;
}
public void setCompetencias(List<CompetenciaEntity> competencias) {
this.competencias = competencias;
}
@Override
public String toString() {
return nome == null ? "" : nome;
}
}
PapelEntity.hbm.xml
Code:
<hibernate-mapping>
<class name="br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity"
table="PAPEL" proxy="br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity">
<id name="id" column="ID" type="int" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">papel_id_seq</param>
</generator>
</id>
<property name="nome" type="string" update="true" insert="true">
<column name="NOME" length="60" not-null="true" unique="true" sql-type="VARCHAR" />
</property>
<property name="descricao" type="string" update="true" insert="true">
<column name="DESCRICAO" length="200" not-null="false" unique="false" sql-type="VARCHAR" />
</property>
<bag name="competencias" table="PAPELCOMPETENCIA" lazy="false" cascade="none">
<key column="PAPEL"></key>
<many-to-many class="br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity"
column="COMPETENCIA"
outer-join="auto" />
</bag>
</class>
</hibernate-mapping>
CompetenciaEntity.class
Code:
/**
* @hibernate.class table = "COMPETENCIA"
* proxy = "br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity"
*/
public class CompetenciaEntity extends AbstractEntity {
private String nome;
private CategoriaCompetenciaEntity categoria;
/**
* @hibernate.id type="int" column="ID" unsaved-value="-1" generator-class="sequence"
* @hibernate.generator-param name="sequence" value="competencia_id_seq"
*/
@Override
public int getId() {
return super.getId();
}
/**
* @hibernate.property type = "string"
* @hibernate.column length = "60" name = "NOME" not-null = "true" sql-type =
* "VARCHAR" unique = "true"
*/
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
/**
* @hibernate.many-to-one cascade = "save-update" class =
* "br.com.sensys.odyssea.servidor.model.entity.rh.CategoriaCompetenciaEntity"
* column = "CATEGORIA" not-null="true"
*/
public CategoriaCompetenciaEntity getCategoria() {
return categoria;
}
public void setCategoria(CategoriaCompetenciaEntity categoria) {
this.categoria = categoria;
}
@Override
public String toString() {
return (this.nome == null ? "" : this.nome);
}
}
CompetenciaEntity.hbm.xml
Code:
<hibernate-mapping>
<class name="br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity"
table="COMPETENCIA"
proxy="br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity">
<id name="id" column="ID" type="int" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">competencia_id_seq</param>
</generator>
</id>
<property name="nome" type="string" update="true" insert="true">
<column name="NOME" length="60" not-null="true" unique="true" sql-type="VARCHAR" />
</property>
<many-to-one name="categoria"
class="br.com.sensys.odyssea.servidor.model.entity.rh.CategoriaCompetenciaEntity"
cascade="save-update" outer-join="auto" update="true"
insert="true" column="CATEGORIA" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Methods find PapelEntity and initialize collection CompetenciaEntity (are POJO)
Code:
public List findAll(String orderBy) throws DAOException {
List<PapelEntity> entities = null;
try {
Criteria crit = HibernateUtil.getSession().createCriteria(PapelEntity.class);
if (orderBy != null && !orderBy.trim().equals("")) {
crit.addOrder(Order.asc(orderBy));
}
entities = crit.list();
initialize(entities);
} catch (HibernateException e) {
throw chainException(e);
} catch (NamingException e) {
throw new DAOException(e);
}
return entities;
}
protected void initialize(List<PapelEntity> entities, Integer usc) throws HibernateException, DAOException {
if(entities == null)
return;
for (PapelEntity entity : entities) {
if(!Hibernate.isInitialized(entity.getCompetencias()))
Hibernate.initialize(entity.getCompetencias());
}
}
Full stack trace of any exception that occurs:Code:
2005-10-06 09:19:46,423 [org.hibernate.LazyInitializationException] ERROR - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
at br.com.sensys.odyssea.servidor.model.entity.rh.CompetenciaEntity$$EnhancerByCGLIB$$34686b12.equals(<generated>)
at java.util.Vector.indexOf(Vector.java:364)
at java.util.Vector.contains(Vector.java:322)
at javax.swing.DefaultListModel.contains(DefaultListModel.java:174)
at br.com.sensys.odyssea.cliente.view.ambiente.gestao.colaborador.gui.PanelHabilitacao.containsCompetencias(PanelHabilitacao.java:157)
at br.com.sensys.odyssea.cliente.view.ambiente.gestao.colaborador.gui.PanelHabilitacao.updatePapeisState(PanelHabilitacao.java:179)
at br.com.sensys.odyssea.cliente.view.ambiente.gestao.colaborador.gui.PanelHabilitacao.addPapeis(PanelHabilitacao.java:110)
at br.com.sensys.odyssea.cliente.view.ambiente.gestao.colaborador.gui.ColaboradorGUI.refreshList(ColaboradorGUI.java:203)
at jpotatoes.view.DispatcherManter.selecionarAction(DispatcherManter.java:885)
at br.com.sensys.odyssea.cliente.view.ambiente.gestao.colaborador.ColaboradorDispatcher.selecionarAction(ColaboradorDispatcher.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at jpotatoes.view.AbstractDispatcher.execute(AbstractDispatcher.java:59)
at jpotatoes.view.gui.AbstractGUI.actionPerformed(AbstractGUI.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Name and version of the database you are using: Postgresql 7.4
The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code:
2005-10-06 09:16:29,731 DEBUG [org.hibernate.pretty.Printer] listing entities:
2005-10-06 09:16:29,731 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Diretor Comercial, descricao=, id=2, competencias=<uninitialized>}
2005-10-06 09:16:29,731 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Gerente de Markting, descricao=, id=8, competencias=<uninitialized>}
2005-10-06 09:16:29,731 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Gerente de Projetos, descricao=, id=3, competencias=<uninitialized>}
2005-10-06 09:16:29,731 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Adimistrador, descricao=, id=5, competencias=<uninitialized>}
2005-10-06 09:16:29,732 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Gerente de desenvolvimento, descricao=, id=1, competencias=<uninitialized>}
2005-10-06 09:16:29,732 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Programador Java, descricao=, id=4, competencias=<uninitialized>}
2005-10-06 09:16:29,732 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Gerente de Vendas, descricao=, id=7, competencias=<uninitialized>}
2005-10-06 09:16:29,733 DEBUG [org.hibernate.pretty.Printer] br.com.sensys.odyssea.servidor.model.entity.rh.PapelEntity{nome=Testador, descricao=, id=6, competencias=<uninitialized>}
2005-10-06 09:16:29,733 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] executing flush
2005-10-06 09:16:29,733 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] post flush
2005-10-06 09:16:29,733 DEBUG [org.hibernate.transaction.CacheSynchronization] transaction before completion callback
2005-10-06 09:16:29,733 DEBUG [org.hibernate.jdbc.JDBCContext] before transaction completion
2005-10-06 09:16:29,733 DEBUG [org.hibernate.impl.SessionImpl] before transaction completion
2005-10-06 09:16:29,735 DEBUG [org.hibernate.transaction.CacheSynchronization] transaction after completion callback, status: 3
2005-10-06 09:16:29,735 DEBUG [org.hibernate.jdbc.JDBCContext] after transaction completion
2005-10-06 09:16:29,735 DEBUG [org.hibernate.impl.SessionImpl] after transaction completion
2005-10-06 09:16:29,735 DEBUG [org.hibernate.transaction.JTATransaction] Committed JTA UserTransaction
2005-10-06 09:16:29,735 DEBUG [org.hibernate.impl.SessionImpl] closing session
2005-10-06 09:16:29,735 DEBUG [org.hibernate.jdbc.ConnectionManager] closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2005-10-06 09:16:29,737 DEBUG [org.hibernate.jdbc.JDBCContext] after transaction completion
2005-10-06 09:16:29,737 DEBUG [org.hibernate.impl.SessionImpl] after transaction completion