Bom galera estou com uma dificuldade eu tenho uma tabela que tem uma fk de outra tabela nela, e essa fk eh obrigatoria, porem, o meu metodo getEnviroment recebe string que é o nome do ambiente, dia eu preciso pegar o nome desse ambiente e colocar em um sistema porem nesse sistema eu preciso setar o codigo do ambiente porem nao estou conseguindo ja coloquei criteria e nao esta funcionando
ServerDAO dao = new ServerDAO();
Server s = new Server();
EnviromentDAO edao = new EnviromentDAO();
s.setIp("192");
s.setName("Servidor Lixo");
Enviroment e = edao.getEnviroment("Desenvolvimento");
s.setEnviroment(e);
dao.insert(s);
erro
18250 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
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:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at br.com.md2net.gi.dao.ServerDAO.insert(ServerDAO.java:34)
at br.com.md2net.gi.teste.TesteDAO.main(TesteDAO.java:36)
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("GI"."SERVER"."CODE_ENVIROMENT")
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8739)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 9 more
public Enviroment getEnviroment(String type){
try{
logger.info("Obtendo ambiente código " + type + " do banco de dados.");
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Criteria c = session.createCriteria(Enviroment.class);
c.add( Restrictions.eq("tipo", type)).list();
List<Enviroment> e = c.list();
sessionFactory.close();
logger.info("Ambiente obtido com sucesso.");
return e.get(0);
}catch (Exception ex){
logger.error("Erro ao obter ambiente com código " + type + ". Motivo:\n" + ex.getMessage());
}
return null;
}
