Fala pessoal blza ?
Eu tenho um objeto da classe Sistema e dentro dele tem um Set de objetos da classe Direito. O que quero fazer é remover um objeto desse Set e refletir essa alteração no banco, entretanto tou recebendo esse exception:
org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [br.com.redeamazonica.corp.model.Sistema.direitos#2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1314)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:84)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at br.com.redeamazonica.corp.testes.Testes.excluir(Testes.java:158)
at br.com.redeamazonica.corp.testes.Testes.main(Testes.java:38)
Caused by: java.sql.SQLException: Cannot insert the value NULL into column ‘cod_sistema’, table ‘ControleAcesso.ControleAcesso.Direito’; column does not allow nulls. UPDATE fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2816)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2254)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:631)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:505)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1283)
… 11 more
Esses são os mapeamentos:
Classe Sistema
<class name="Sistema">
<id name="cod_sistema" type="integer">
<generator class="increment"></generator>
</id>
<property name="nome" type="string" length="50" not-null="true"></property>
<property name="nomeBanco" type="string" length="15" not-null="true"></property>
<set name="controles" >
<key column="cod_sistema"></key>
<one-to-many class="Controle" />
</set>
<set name="direitos" lazy="false">
<key column="cod_sistema"></key>
<one-to-many class="Direito" />
</set>
</class>
Classe Direito
<class name="Direito">
<id name="cod_direito" type="integer">
<generator class="increment"></generator>
</id>
<property name="nome" type="string" length="50" not-null="true"></property>
<many-to-one name="sistema" column="cod_sistema" class="Sistema" not-null="true" lazy="false"></many-to-one>
<set name="perfis" table="PerfilDireito">
<key column="cod_direito"></key>
<many-to-many column="cod_perfil" class="Perfil"></many-to-many>
</set>
<set name="controles" >
<key column="cod_direito"></key>
<one-to-many class="Controle"/>
</set>
</class>
Este é o código de testes:
Session session = null;
Transaction transaction = null;
try
{
session = HibernateUtil2.getSessionFactory().openSession();
transaction = session.beginTransaction();
Sistema sistema = (Sistema) session.get(Sistema.class, 2);
Direito direitoRemover = null;
for (Direito direito : sistema.getDireitos())
{
if (direito.getCod_direito() == 8)
{
direitoRemover = direito;
break;
}
}
sistema.getDireitos().remove(direitoRemover);
session.save(sistema);
transaction.commit();
System.out.println("\nOK!!!");
}
catch (Exception e)
{
transaction.rollback();
e.printStackTrace();
}
Aguardo respostas. Valeu pessoal.