Hibernate with bank

Asked

Viewed 71 times

1

I am using creating a program that adds an information in a database through Hibernate, almost an hour ago it was working perfectly, but now when I run the code generates these errors.

org.hibernate.Exception.Constraintviolationexception: could not execute statement at org.hibernate.Exception.internal.Sqlexceptiontypedelegate.Convert(Sqlexceptiontypedelegate.java:59) at org.hibernate.Exception.internal.Standardsqlexceptionconverter.Convert(Standardsqlexceptionconverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:109) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:95) at org.hibernate.engine.jdbc.Internal.ResultSetReturnImpl.executeUpdate(Resultsetreturnimpl.java:207) at org.hibernate.id.Identitygenerator$Getgeneratedkeysdelegate.executeAndExtract(Identitygenerator.java:84) at org.hibernate.id.insert.Abstractreturningdelegate.performInsert(Abstractreturningdelegate.java:42) at org.hibernate.persister.entity.Abstractentitypersister.Insert(Abstractentitypersister.java:2791) at org.hibernate.persister.entity.Abstractentitypersister.Insert(Abstractentitypersister.java:3361) at org.hibernate.action.internal.Entityidentityinsertaction.execute(Entityidentityinsertaction.java:81) at org.hibernate.engine.spi.Actionqueue.execute(Actionqueue.java:478) at org.hibernate.engine.spi.Actionqueue.addResolvedEntityInsertAction(Actionqueue.java:179) at org.hibernate.engine.spi.Actionqueue.addInsertAction(Actionqueue.java:163) at org.hibernate.engine.spi.Actionqueue.addAction(Actionqueue.java:198) at org.hibernate.Event.internal.Abstractsaveeventlistener.addInsertAction(Abstractsaveeventlistener.java:317) at org.hibernate.Event.internal.Abstractsaveeventlistener.performSaveOrReplicate(Abstractsaveeventlistener.java:272) at org.hibernate.Event.internal.Abstractsaveeventlistener.performSave(Abstractsaveeventlistener.java:178) at org.hibernate.Event.internal.Abstractsaveeventlistener.saveWithGeneratedId(Abstractsaveeventlistener.java:109) at org.hibernate.Event.internal.Defaultsaveorupdateeventlistener.saveWithGeneratedOrRequestedId(Defaultsaveorupdateeventlistener.java:192) at org.hibernate.Event.internal.Defaultsaveeventlistener.saveWithGeneratedOrRequestedId(Defaultsaveeventlistener.java:38) at org.hibernate.Event.internal.Defaultsaveorupdateeventlistener.entityIsTransient(Defaultsaveorupdateeventlistener.java:177) at org.hibernate.Event.internal.Defaultsaveeventlistener.performSaveOrUpdate(Defaultsaveeventlistener.java:32) at org.hibernate.Event.internal.Defaultsaveorupdateeventlistener.onSaveOrUpdate(Defaultsaveorupdateeventlistener.java:73) at org.hibernate.Internal.SessionImpl.fireSave(Sessionimpl.java:678) at org.hibernate.Internal.SessionImpl.save(Sessionimpl.java:670) at org.hibernate.Internal.SessionImpl.save(Sessionimpl.java:665) at br.com.iesam.horario.utils.Teste.popularBanco(Test.java:81) at br.com.iesam.horario.utils.Teste.main(Test.java:20) Caused by: com.mysql.jdbc.exceptions.jdbc4.Mysqlintegrityconstraintviolationexception: Cannot add or update a Child Row: a Foreign key Cont strainfails (horarioprofessores.disciplinas, CONSTRAINT FKiica8mlykvmu920xf6ld3g66k FOREIGN KEY (idDisciplina) REFERENCES turmas (idTurma)) at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Nativeconstructoraccessorimpl.java:62) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Delegatingconstructoraccessorimpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:934) at com.mysql.jdbc.MysqlIO.checkErrorPacket(Mysqlio.java:3870) at com.mysql.jdbc.MysqlIO.checkErrorPacket(Mysqlio.java:3806) at com.mysql.jdbc.MysqlIO.sendCommand(Mysqlio.java:2470) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(Mysqlio.java:2617) com.mysql.jdbc.ConnectionImpl.execSQL(Connectionimpl.java:2550) com.mysql.jdbc.PreparedStatement.executeInternal(Preparedstatement.java:1861) com.mysql.jdbc.PreparedStatement.executeUpdateInternal(Preparedstatement.java:2073) com.mysql.jdbc.PreparedStatement.executeUpdateInternal(Preparedstatement.java:2009) com.mysql.jdbc.PreparedStatement.executeLargeUpdate(Preparedstatement.java:5094) com.mysql.jdbc.PreparedStatement.executeUpdate(Preparedstatement.java:1994) at org.hibernate.engine.jdbc.Internal.ResultSetReturnImpl.executeUpdate(Resultsetreturnimpl.java:204) ... 23 more.

the code follows this example

public class Teste {
    private static Session session;
    public static void main(String[] args) {
	session = Conexao.getSession();// Add no banco de dados
	
	popularBanco();
	
	Conexao.close();
    }
  
  
  
  
   public static void popularBanco() {
        Transaction tx = session.beginTransaction();
	try {
            	    
//            professores.add(new Professores("João João"));
//            professores.add(new Professores("Maria Maria"));	    
//            session.save(professores);
            
            // adicionar horarios
            Horarios hora[][][] = new Horarios[5][3][5];
            for (int dia = 0 ; dia < 5 ; dia++) {
                for (int turno = 0 ; turno < 3 ; turno++) {
                    for (int horario = 0 ; horario < 5 ; horario ++) {
                        hora[dia][turno][horario] = new Horarios(dia,turno,horario);
                        session.save(hora[dia][turno][horario]);
                    }
                }
            }

                        
             ProfessoresHorarios prof1 = new ProfessoresHorarios(AderbalMaia, hora[0][1][0], 2);
             session.save(prof1);
             ProfessoresHorarios prof2 = new ProfessoresHorarios(ElionaiSobrinho, hora[2][1][1], 3);
             session.save(prof2);
             ProfessoresHorarios prof3= new ProfessoresHorarios(ElionaiSobrinho, hora[1][0][0], 1);
             session.save(prof3);
             ProfessoresHorarios prof4 = new ProfessoresHorarios(ElionaiSobrinho, hora[4][1][1], 1);
             session.save(prof4);
      
	} catch (Exception e) {
	    e.printStackTrace();
	    tx.rollback();
	}
    }
    
}

  • Are you trying to change any existing record ?

1 answer

0


Miguel by the error log you are violating a Constraint, by the way a foreign key, see

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot add or update a child row: a foreign key constraint fails 
(horarioprofessores.disciplinas, CONSTRAINT FKiica8mlykvmu920xf6ld3g66k FOREIGN KEY 
(idDisciplina) REFERENCES turmas (idTurma)) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

from a look here lecturers. you are violating a rule you created in the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.