Jpa Hibernate merge error

Asked

Viewed 711 times

0

Error

Hibernate: select SEQ_COUNT from SEQUENCE_TABLE Where SEQ_NAME = 'EMP_SEQ' for update Hibernate: update SEQUENCE_TABLE set SEQ_COUNT = ? Where SEQ_COUNT = ? and SEQ_NAME = 'EMP_SEQ' Hibernate: Insert into contacts (email, name, id) values (?, ?, ?) jul 15, 2016 3:13:20 PM org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions WARN: SQL Error: 1364, Sqlstate: HY000 jul 15, 2016 3:13:20 PM org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions ERROR: Field 'created_at' doesn’t have a default value jul 15, 2016 3:13:20 PM org.hibernate.engine.jdbc.batch.internal.Abstractbatchimpl release INFO: HHH000010: On release of batch it still contained JDBC statements jul 15, 2016 3:13:20 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper$Standardwarninghandler logWarning WARN: SQL Warning Code: 1364, Sqlstate: HY000 jul 15, 2016 3:13:20 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper$Standardwarninghandler logWarning WARN: Field 'created_at' doesn’t have a default value jul 15, 2016 3:13:20 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper$Standardwarninghandler logWarning WARN: SQL Warning Code: 1364, Sqlstate: HY000 jul 15, 2016 3:13:20 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper$Standardwarninghandler logWarning WARN: Field 'updated_at' doesn’t have a default value javax.persistence.Rollbackexception: Error while committing the transaction at org.hibernate.jpa.internal.Transactionimpl.commit(Transactionimpl.java:94) at br.com.contact pi1.contacto.dao.ContactoJpaDAO.saveOrUpdate(Contactjava:42) at br.com.contact pi1.contactApiController.findAll(Contactpicontroller.java:24) at sun.reflect.Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect..Nativemethodaccessorimpl.invoke(Nativemethodaccessorimpl.java:62) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Delegatingmethodaccessorimpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Invocablehandlermethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invocablehandlermethod.java:137) at org.springframework.web.servlet.mvc.method.Annotation.ServletInvocableHandlerMethod.invokeAndHandle(Servletinvocablehandlermethod.java:111) at org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(Requestmappinghandleradapter.java:806) at org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdapter.handleInternal(Requestmappinghandleradapter.java:729) at org.springframework.web.servlet.mvc.method.Abstracthandlermethodadapter.Handle(Abstracthandlermethodadapter.java:85) at org.springframework.web.servlet.Dispatcherservlet.doDispatch(Dispatcherservlet.java:959) at org.springframework.web.servlet.Dispatcherservlet.doService(Dispatcherservlet.java:893) at org.springframework.web.servlet.Frameworkservlet.processRequest(Frameworkservlet.java:970) at org.springframework.web.servlet.Frameworkservlet.doPost(Frameworkservlet.java:872) at javax.servlet.http.HttpServlet.service(Httpservlet.java:648) at org.springframework.web.servlet.Frameworkservlet.service(Frameworkservlet.java:846) at javax.servlet.http.HttpServlet.service(Httpservlet.java:729) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:292) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:207) at org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:52) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:240) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:207) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:212) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:106) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:502) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:141) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:79) at org.apache.Catalina.valves.Abstractaccesslogvalve.invoke(Abstractaccesslogvalve.java:616) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:88) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:528) at org.apache.Coyote.http11.Abstracthttp11processor.process(Abstracthttp11processor.java:1099) at org.apache.Coyote.Abstractprotocol$Abstractconnectionhandler.process(Abstractprotocol.java:670) at org.apache.Tomcat.util.net.Nioendpoint$Socketprocessor.doRun(Nioendpoint.java:1520) at org.apache.Tomcat.util.net.Nioendpoint$Socketprocessor.run(Nioendpoint.java:1476) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Threadpoolexecutor.java:1142) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Threadpoolexecutor.java:617) at org.apache.Tomcat.util.threads.Taskthread$Wrappingrunnable.run(Taskthread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: javax.persistence.Persistenceexception: org.hibernate.Exception.Genericjdbcexception: could not execute statement at org.hibernate.jpa.spi.Abstractentitymanagerimpl.Convert(Abstractentitymanagerimpl.java:1763) at org.hibernate.jpa.spi.Abstractentitymanagerimpl.Convert(Abstractentitymanagerimpl.java:1677) at org.hibernate.jpa.internal.Transactionimpl.commit(Transactionimpl.java:82) ... 40 more Caused by: org.hibernate.Exception.Genericjdbcexception: could not execute statement at org.hibernate.Exception.internal.Standardsqlexceptionconverter.Convert(Standardsqlexceptionconverter.java:54) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:112) at org.hibernate.engine.jdbc.Internal.ResultSetReturnImpl.executeUpdate(Resultsetreturnimpl.java:211) at org.hibernate.engine.jdbc.batch.internal.Nonbatchingbatch.addToBatch(Nonbatchingbatch.java:62) at org.hibernate.persister.entity.Abstractentitypersister.Insert(Abstractentitypersister.java:3124) at org.hibernate.persister.entity.Abstractentitypersister.Insert(Abstractentitypersister.java:3581) at org.hibernate.action.internal.Entityinsertaction.execute(Entityinsertaction.java:104) at org.hibernate.engine.spi.Actionqueue.executeActions(Actionqueue.java:465) at org.hibernate.engine.spi.Actionqueue.executeActions(Actionqueue.java:351) at org.hibernate.Event.internal.Abstractflushingeventlistener.performExecutions(Abstractflushingeventlistener.java:350) at org.hibernate.Event.internal.Defaultflusheventlistener.onFlush(Defaultflusheventlistener.java:56) at org.hibernate.Internal.SessionImpl.flush(Sessionimpl.java:1258) at org.hibernate.Internal.SessionImpl.managedFlush(Sessionimpl.java:425) at org.hibernate.engine.transaction.Internal.jdbc.Jdbctransaction.beforeTransactionCommit(Jdbctransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(Abstracttransactionimpl.java:177) at org.hibernate.jpa.internal.Transactionimpl.commit(Transactionimpl.java:77) ... 40 more Caused by: java.sql.Sqlexception: Field 'created_at' doesn’t have a default value at com.mysql.jdbc.SQLError.createSQLException(Sqlerror.java:957))

Model

Obs: @Entity @Table(name = "contatos") are on the declaration of the class.

public class Contato {

    @Id
    @TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME",
    valueColumnName="SEQ_COUNT", pkColumnValue="EMP_SEQ")
    @GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")
    @Column(name = "id", nullable = false)
    private int id;

    @Column(name = "nome", nullable = false)
    private String nome;

    @Column(name = "email", nullable = false)
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

DAO

public class ContatoJpaDAO {

    private final EntityManagerFactory factory;
    private final EntityManager entityManager;

    private Contato contato;

    public ContatoJpaDAO() {
        this.factory = Persistence.createEntityManagerFactory("contato");
        this.entityManager = factory.createEntityManager();
    }

    public void saveOrUpdate(Contato contato) {
        try {

            this.entityManager.getTransaction().begin();
            this.entityManager.merge(contato);
            this.entityManager.getTransaction().commit();
            this.entityManager.close();

        } catch (Exception e) {
            e.printStackTrace();
            this.entityManager.getTransaction().rollback();
        }
    }
}

Controller

@CrossOrigin(origins = "*")
@RestController
public class ContatoApiController {
    Contato contato;
    private final ContatoJpaDAO contatoJpaDAO = new ContatoJpaDAO();

    @RequestMapping(value = "/contato", headers="Accept=application/json", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    private void findAll(@RequestBody Contato contato) {
         contatoJpaDAO.saveOrUpdate(contato);
    }
}
  • Is it possible to remove parts of the question that do not matter to the problem? As it is has too many things, difficult to have time to read all...

2 answers

1


It is even shameful to say hehehe but it was the following:

i was studying Rubyonrails and used the bank created by him I remembered that Rails creates 2 more fields update_at and the created_ad so I was not putting nor a value to them hehe :) it was only I change bank that worked. Thanks to all.

0

Cara I believe that in his saveOrUpdate method something like testing to see if the contact already exists, to merge, because the merge is for something already in the bank. If it does not exist should be used the persist follows the refactored code test there and anything put in the comments!

public void saveOrUpdate(Contato contato) {
try {

    this.entityManager.getTransaction().begin();
    if(contato.getId != null)//teste para ver se de fato existe o id se existe merge senão persist
    {
        this.entityManager.merge(contato);
    }
    this.entityManager.persist(cliente);
    this.entityManager.getTransaction().commit();
    this.entityManager.close();

} catch (Exception e) {
    e.printStackTrace();
    this.entityManager.getTransaction().rollback();
}
}
  • Opa thank you so much for having answered I put your code but it did not work gave the same error, the merge if I am not mistaken it makes the search if it exists it updates if it does not exist it creates a new one and that?

Browser other questions tagged

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