Solucionado (ver solução)
Solucionado
(ver solução)
9
respostas

Erro de Persistência ao gravar

HTTP Status 500 - javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence

type Exception report

message javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause

javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) javax.faces.component.UICommand.broadcast(UICommand.java:315) javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152) bandas.bean.MatriculaBean.criarMatricula(MatriculaBean.java:33) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.apache.el.parser.AstValue.invoke(AstValue.java:278) org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273) com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) javax.faces.component.UICommand.broadcast(UICommand.java:315) javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause

org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:143) org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126) org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55) org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34) org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:56) org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125) org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:42) org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:417) org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:774) org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747) org.hibernate.internal.SessionImpl.persist(SessionImpl.java:752) org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) bandas.bean.MatriculaBean.criarMatricula(MatriculaBean.java:33) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.apache.el.parser.AstValue.invoke(AstValue.java:278) org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273) com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) javax.faces.component.UICommand.broadcast(UICommand.java:315) javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791) javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256) com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.64 logs.

9 respostas

Oi Ederson,

Você pode postar o código da sua classe modelo e da sua classe DAO?

Concerteza Douglas, Obrigado pela ajuda!

Modelo Evento: package bandas.model;

import java.io.Serializable; import java.util.ArrayList; import java.util.List;

import javax.persistence.*;

@Entity public class Evento implements Serializable {

private static final long serialVersionUID = 1L; @Id @GeneratedValue private int id; private String local; @ManyToMany private List artistas = new ArrayList();

public void relacionarArtista(Artista artista) { this.artistas.add(artista); }

public int getId() { return id; }

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

public String getLocal() { return local; }

public void setLocal(String local) { this.local = local; }

public List getArtistas() { return artistas; } }

Modelo artista:

package bandas.model;

import javax.persistence.*;

@Entity public class Artista {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String nome;

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; } }

DAO:

package bandas.util;

import java.util.List;

import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaQuery;

public class BandasDAO { private final Class classe;

public BandasDAO(Class classe) { this.classe = classe; }

public void adiciona(T t) {

EntityManager manager = new BandasJPAUtil().getManager(); manager.getTransaction().begin(); manager.persist(t); manager.getTransaction().commit(); manager.close(); }

public void remove(T t) { EntityManager em = new BandasJPAUtil().getManager(); em.getTransaction().begin();

em.remove(em.merge(t));

em.getTransaction().commit(); em.close(); }

public void atualiza(T t) { EntityManager manager = new BandasJPAUtil().getManager(); manager.getTransaction().begin();

manager.merge(t);

manager.getTransaction().commit(); manager.close(); }

public List listaTodos() { EntityManager em = new BandasJPAUtil().getManager(); CriteriaQuery query = em.getCriteriaBuilder().createQuery(classe); query.select(query.from(classe));

List lista = em.createQuery(query).getResultList();

em.close(); return lista; }

public T buscaPorId(Integer id) { EntityManager em = new BandasJPAUtil().getManager(); T instancia = em.find(classe, id); em.close(); return instancia; }

public int contaTodos() { EntityManager em = new BandasJPAUtil().getManager(); long result = (Long) em.createQuery("select count(n) from livro n") .getSingleResult(); em.close();

return (int) result; }

public List listaTodosPaginada(int firstResult, int maxResults) { EntityManager em = new BandasJPAUtil().getManager(); CriteriaQuery query = em.getCriteriaBuilder().createQuery(classe); query.select(query.from(classe));

List lista = em.createQuery(query).setFirstResult(firstResult) .setMaxResults(maxResults).getResultList();

em.close(); return lista; }

}

O Bean do Artista está funcionando normalmente. Apenas o Evento que não. Se poder ajudar agradeço. Valeu!!!

Bean Evento: package bandas.bean;

import java.io.Serializable; import java.util.List;

import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.swing.JOptionPane;

import bandas.model.Artista; import bandas.model.Evento; import bandas.util.BandasDAO;

@ManagedBean @ViewScoped public class EventoBean implements Serializable {

private static final long serialVersionUID = 1L; private Evento evento = new Evento(); private int artistaId;

public Evento getEvento() { return this.evento; }

public List getArtistas() { return new BandasDAO(Artista.class).listaTodos(); }

public void relacionarArtista() {

try { Artista artista = new BandasDAO(Artista.class) .buscaPorId(this.artistaId); this.evento.relacionarArtista(artista); testeBotao(); } catch (Exception e) { throw new RuntimeException("Não criado relacionamento com o artista"); } }

public void testeBotao() { JOptionPane.showMessageDialog(null, "Botão Pressionado"); }

public void publicarEvento() {

System.out.println(" >> " + evento.getLocal()); if (evento.getArtistas().isEmpty()) { throw new RuntimeException("Artistas não selecionados"); }

try { new BandasDAO(Evento.class).adiciona(this.evento); this.evento = new Evento(); } catch (Exception e) { throw new RuntimeException("Evento Não Publicado"); } }

public int getArtistaId() { return artistaId; }

public void setArtistaId(int artistaId) { this.artistaId = artistaId; } }

Ederson, tenta adicionar junto a anotação, o strategy na classe Evento

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

eu fiz isso, mas o erro persiste rsrs estou tentando achar no google, mas está dificil. nem os caras da alura nunca ajudam.

solução!

Ederson, bom, tenta apagar as tabelas para que sejam criadas novamente, mas agora com o strategy setado ou tente diretamente utilizar esse sql, assumindo que você está usando o mysql.

insert into mysqldb.hibernate_sequence(next_val) values(0)

CARA... ESSE LANCE DE EXCLUIR AS TABELAS DEU CERTO!!!! MUITO OBRIGADO MESMO E QUE DEUS ABENÇOE A SUA VIDA!!! VALEU MANO!!! EU PERDI MUITO TEMPO COM ESSE DETALHE RSRS.