2
respostas

Estratégia IDENTITY para geração o Id

@Id @GeneratedValue(strategy=GenerationType.IDENTIT) private Integer id;

Erro:

Exception in thread "main" java.lang.ClassCastException: class org.hibernate.id.IdentifierGeneratorHelper$2 cannot be cast to class java.lang.Integer (org.hibernate.id.IdentifierGeneratorHelper$2 is in unnamed module of loader 'app'; java.lang.Integer is in module java.base of loader 'bootstrap') at org.hibernate.type.descriptor.java.internal.IntegerJavaDescriptor.unwrap(IntegerJavaDescriptor.java:26) at org.hibernate.type.descriptor.sql.spi.IntegerSqlDescriptor$1.doBind(IntegerSqlDescriptor.java:67) at org.hibernate.sql.AbstractJdbcValueBinder.bind(AbstractJdbcValueBinder.java:69) at org.hibernate.sql.ast.tree.expression.LiteralParameter.bindParameterValue(LiteralParameter.java:86) at org.hibernate.sql.exec.internal.JdbcMutationExecutorImpl.execute(JdbcMutationExecutorImpl.java:61) at org.hibernate.sql.exec.internal.JdbcMutationExecutorImpl.execute(JdbcMutationExecutorImpl.java:109) at org.hibernate.metamodel.model.domain.internal.entity.SingleTableEntityTypeDescriptor.executeOperation(SingleTableEntityTypeDescriptor.java:768) at org.hibernate.metamodel.model.domain.internal.entity.SingleTableEntityTypeDescriptor.executeInsert(SingleTableEntityTypeDescriptor.java:384) at org.hibernate.metamodel.model.domain.internal.entity.SingleTableEntityTypeDescriptor.executeInsert(SingleTableEntityTypeDescriptor.java:376) at org.hibernate.metamodel.model.domain.internal.entity.SingleTableEntityTypeDescriptor.insertInternal(SingleTableEntityTypeDescriptor.java:204) at org.hibernate.metamodel.model.domain.spi.AbstractEntityTypeDescriptor.insert(AbstractEntityTypeDescriptor.java:885) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:79) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:643) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:278) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:259) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:313) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:392) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:301) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:208) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:139) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:59) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:776) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:761) at br.com.caelum.financas.teste.TesteMovimentacaoComCategoria.main(TesteMovimentacaoComCategoria.java:51)

2 respostas

Bom dia Augusto, pode detalhar mais o erro? Em que situação ocorre?

Quando uso a estratégia GenerationType.IDENTIT ocorre esse erro aí. Eu testei com GenerationType.AUTO e não ocorre erro.