Problema Inserção Hibernate

8 respostas
hibernatemysqljava
F

Estou com um problema de inserção de veículos, porém o cadastro de pessoas está funcionando normalmente. Segue os códigos abaixo:

Método add:

public void addVeiculo(Veiculo veiculo) {
        Transaction tx = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            session.save(veiculo);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

Tela Cadastro de Veículo:

Associado associado = new Associado();
        Veiculo veiculo = new Veiculo();
        VeiculoDao vdao = new VeiculoDao();

        Associado a = (Associado) cmbfk_associado.getSelectedItem();
        veiculo.setAssociado(new Associado());
         veiculo.getAssociado().setIdAssociado(a.getIdAssociado());   
         
        veiculo.setIdveiculo(Integer.parseInt(txtId.getText()));
        veiculo.setPlaca(txtPlacaV.getText());
        veiculo.setRenavam(txtRenavamV.getText());
        veiculo.setModelo(txtModeloV.getText());
        veiculo.setChassi(txtChassiV.getText());
        veiculo.setAno(Integer.parseInt(txtAnoV.getText()));
        veiculo.setCor(txtCorV.getText());
        veiculo.setAssociado(veiculo.getAssociado());
        veiculo.setAssociado(associado);
        vdao.addVeiculo(veiculo);

Os Erros:

run:
mai 06, 2019 3:28:02 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
mai 06, 2019 3:28:02 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
mai 06, 2019 3:28:02 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Model/Engate.hbm.xml
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Model/Login.hbm.xml
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Model/Veiculo.hbm.xml
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Model/Associado.hbm.xml
mai 06, 2019 3:28:02 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
mai 06, 2019 3:28:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
mai 06, 2019 3:28:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/sistema?zeroDateTimeBehavior=convertToNull]
mai 06, 2019 3:28:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root}
mai 06, 2019 3:28:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
mai 06, 2019 3:28:02 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
mai 06, 2019 3:28:03 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
mai 06, 2019 3:28:03 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
mai 06, 2019 3:28:03 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
mai 06, 2019 3:28:04 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
	Unsaved transient entity: ([Model.Associado#0])
	Dependent entities: ([[Model.Veiculo#<null>]])
	Non-nullable association(s): ([Model.Veiculo.associado])
org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : Model.Veiculo.associado -> Model.Associado
	at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:137)
	at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:314)
	at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:654)
	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:713)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
	at Dao.VeiculoDao.addVeiculo(VeiculoDao.java:24)
	at View.Cad_Veiculo.btnCadVeiculoActionPerformed(Cad_Veiculo.java:706)
	at View.Cad_Veiculo.access$1500(Cad_Veiculo.java:21)
	at View.Cad_Veiculo$16.actionPerformed(Cad_Veiculo.java:618)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6539)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6304)
	at java.awt.Container.processEvent(Container.java:2239)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2297)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
	at java.awt.Container.dispatchEventImpl(Container.java:2283)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
mai 06, 2019 3:28:04 PM org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in Model.Veiculo entry (don't flush the Session after an exception occurs)
Exception in thread "AWT-EventQueue-0" org.hibernate.AssertionFailure: null id in Model.Veiculo entry (don't flush the Session after an exception occurs)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:77)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
	at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
	at Dao.VeiculoDao.addVeiculo(VeiculoDao.java:32)
	at View.Cad_Veiculo.btnCadVeiculoActionPerformed(Cad_Veiculo.java:706)
	at View.Cad_Veiculo.access$1500(Cad_Veiculo.java:21)
	at View.Cad_Veiculo$16.actionPerformed(Cad_Veiculo.java:618)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6539)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6304)
	at java.awt.Container.processEvent(Container.java:2239)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2297)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
	at java.awt.Container.dispatchEventImpl(Container.java:2283)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

8 Respostas

Fabio_Dos_Reis

Olhando assim por cima, parece que sua entidade veiculo tem outra classe dentro dele que está nula e o hibernate não consegue saber que entidade é essa, provavelmente é uma composição não setada na entidade veículo.

darlan_machado

É a propriedade associado.

F

Na tabela Veiculo tem uma fk_idassociado referenciando o campo id_associado da tabela Associado, o problema esta na inserção da fk? Estou listando o nome das pessoas cadastradas no combobox na tela de cadastro de Veiculo, pegando o id da pessoa selecionada e tentando inserir na fk juntamente com o restante dos dados do veiculo.

darlan_machado

Cara, você não leu o erro? Veja a mensagem e entenda o que ele diz.

Not-null property references a transient value - transient instance must be saved before current operation : Model.Veiculo.associado -> Model.Associado

O que significa essa mensagem?

F

Desculpe, não sei muito, estou no começo, por isso estou pedindo ajuda de como resolver isso :+1:

darlan_machado

Sugiro, fortemente, que você deixe isso por enquanto e volte algumas páginas.
Primeiro, entender o que é um atributo transiente é fundamental para essa etapa, sem isso, você vai ter sérias dificuldades quanto a isso.

F

Preciso deixar isso pronto o mais rápido possível, mesmo assim obrigado pela ajuda!

darlan_machado

O fato de precisar disso com urgência não interfere no fato de que precisa aprender certas coisas.

Criado 6 de maio de 2019
Ultima resposta 6 de mai. de 2019
Respostas 8
Participantes 3