-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: [SOLVED]hibernate.AnnotationException: Unknown Id.generator
PostPosted: Sat Oct 03, 2009 2:09 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Hi!

The error is..

Code:
init: 
deps-jar: 
Compiling 1 source file to /home/bruno/NetBeansProjects/IntroducaoHibernate01/build/classes 
compile: 
run: 
03/10/2009 00:11:42 org.hibernate.cfg.annotations.Version <clinit> 
INFO: Hibernate Annotations 3.3.1.GA 
03/10/2009 00:11:42 org.hibernate.cfg.Environment <clinit> 
INFO: Hibernate 3.2.5 
03/10/2009 00:11:42 org.hibernate.cfg.Environment <clinit> 
INFO: hibernate.properties not found 
03/10/2009 00:11:42 org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: Bytecode provider name : cglib 
03/10/2009 00:11:42 org.hibernate.cfg.Environment <clinit> 
INFO: using JDK 1.4 java.sql.Timestamp handling 
03/10/2009 00:11:42 org.hibernate.cfg.Configuration configure 
INFO: configuring from resource: /hibernate.cfg.xml 
03/10/2009 00:11:42 org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: Configuration resource: /hibernate.cfg.xml 
03/10/2009 00:11:42 org.hibernate.cfg.AnnotationConfiguration addPackage 
INFO: Mapping package introducaohibernate01 
03/10/2009 00:11:42 org.hibernate.cfg.AnnotationBinder bindPackage 
WARNING: Package not found or wo package-info.java: introducaohibernate01 
03/10/2009 00:11:42 org.hibernate.cfg.Configuration doConfigure 
INFO: Configured SessionFactory: null 
03/10/2009 00:11:42 org.hibernate.cfg.AnnotationBinder bindClass 
INFO: Binding entity from annotated class: introducaohibernate01.Pessoa 
03/10/2009 00:11:42 org.hibernate.cfg.annotations.EntityBinder bindTable 
INFO: Bind entity introducaohibernate01.Pessoa on table pessoa 
Initial SessionFactory creation failed.Unknown Id.generator: codigo_seq 
Exception in thread "main" java.lang.ExceptionInInitializerError 
at util.HibernateUtil.<clinit>(HibernateUtil.java:30) 
at introducaohibernate01.ProfessorDAO.salvarProfessor(ProfessorDAO.java:17) 
at introducaohibernate01.Main.main(Main.java:38) 
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: codigo_seq 
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:428) 
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1908) 
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1281) 
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754) 
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:534) 
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286) 
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859) 
at util.HibernateUtil.<clinit>(HibernateUtil.java:26) 
... 2 more 
Java Result: 1 
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo) 


Pessoa class

Code:
@Entity     
@Table(name="pessoa") 
@Inheritance(strategy = InheritanceType.JOINED) 
public abstract class Pessoa implements Serializable {     
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "codigo_seq") 
   @SequenceGenerator(name = "codigo_seq", sequenceName = "codigo_seq", allocationSize =1) 
   @Id 
   @Column 
   private Integer codigo; 


My Professor class

Code:

@Entity
@Table(name="professor")
public class Professor extends Pessoa {
    @PrimaryKeyJoinColumn(name="matProfessor")
    @Column (name = "mat_professor")
    private Integer matProfessor;


My Aluno class

Code:
@Entity
@Table(name="aluno")
public class Aluno extends Pessoa {
    @PrimaryKeyJoinColumn(name="matAluno")
    @Column (name = "mat_aluno")
    private Integer matAluno;


My SQL code on PostgreSQL 8.3:

Code:
create table pessoa (
codigo serial not null,
nome varchar(80) not null,
data_nascimento varchar(9) not null,
email varchar(45) not null,
endereco varchar(100) not null,
telefone varchar(20) not null,
cpf varchar(12) not null,
identidade varchar(15) not null,
constraint pk_codigo primary key (codigo)
);

create table aluno(
mat_aluno integer not null,
ano_inicio varchar(4) not null,
semestre_inicio integer not null,
constraint pk_mat_aluno primary key (mat_aluno)
) inherits (pessoa);

create table professor(
mat_professor integer not null,
titulacao_maxima varchar(45) not null,
constraint pk_mat_professor primary key (mat_professor)
) inherits (pessoa);


when I insert the data by postgre, works perfectly, but hibernate appears this error.
can anyone help?


Last edited by Guevara on Sun Oct 04, 2009 5:41 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: org.hibernate.AnnotationException: Unknown Id.generator
PostPosted: Sat Oct 03, 2009 3:40 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi Guevara,

I'm not very much into Hibernate Annotations.
Guevara wrote:
Code:
...
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "codigo_seq") 
   @SequenceGenerator(name = "codigo_seq", sequenceName = "codigo_seq", allocationSize =1) 
   @Id 
   @Column 
   private Integer codigo;
...


I'd guess that you need to specify an existing generator in @GeneratedValue and not your sequence's name (Am I right that codigo_seq ios a sequence in your PostgreSQL DB?). You might have the option to extend the Hibernate's built-in generators but then you have to create a class for your codigo_seq.

Check the docs: Mapping identifier properties or id (esp. the first sub-section).

CU
Froestel


Top
 Profile  
 
 Post subject: Re: org.hibernate.AnnotationException: Unknown Id.generator
PostPosted: Sat Oct 03, 2009 7:32 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
Hi Froestel!
I read the entire manual, but did not understand how to use the generator. I tried it.

Code:
@Entity   
    @Table(name="pessoa")
    @Inheritance(strategy = InheritanceType.JOINED)
    @GenericGenerator(name="codigo_seq", strategy = "codigo_seq",
    parameters = {
        @Parameter(name="sequence", value="codigo_seq") })

public abstract class Pessoa implements Serializable {   
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "codigo_seq")
    @Id
    @Column
    private Integer codigo;


The error:

Code:
INFO: building session factory
Initial SessionFactory creation failed.could not instantiate id generator
Exception in thread "main" java.lang.ExceptionInInitializerError
        at util.HibernateUtil.<clinit>(HibernateUtil.java:22)
        at introducaohibernate01.ProfessorDAO.salvarProfessor(ProfessorDAO.java:17)
        at introducaohibernate01.Main.main(Main.java:24)
Caused by: org.hibernate.MappingException: could not instantiate id generator
        at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98)
        at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
        at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
        ... 2 more
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: codigo_seq
        at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:109)
        at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:92)
        ... 7 more
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)



This is the image of the sequence.

Image


Thanks!!


Top
 Profile  
 
 Post subject: Re: org.hibernate.AnnotationException: Unknown Id.generator
PostPosted: Sun Oct 04, 2009 12:30 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
I solve partially the problem, now the error is in the "name" column, is saying that can not be null, but I'm entering the name in the main class.

Main class
Code:
public class Main {

    public static void main(String[] args) {

    try {
             

        //Persistindo Professor

        Professor p = new Professor();
       
        p.setNome("Astor Piazzola");
        p.setDataNascimento("9/11/88");
        p.setEmail("astor@gmail.com");
        p.setEndereco("rua h");
        p.setTelefone("9985-3205");
        p.setCpf("2651114520");
        p.setIdentidade("8504733064");
        p.setMatProfessor(105);
        p.setTitulacaoMaxima("Doutor");   
       
       
        //Persistindo Aluno
        Aluno a = new Aluno();       
       
        a.setNome("Renata Carvalho");
        a.setDataNascimento("3/9/98");
        a.setEmail("renata@gmail.com");
        a.setEndereco("rua z");
        a.setTelefone("9510-7884");
        a.setCpf("1300568844");
        a.setIdentidade("0563034854");
        a.setMatAluno(204);
        a.setAnoInicio("1990");
        a.setSemestreInicio(1);
       
        //Gravando aluno e professor

        ProfessorDAO professorDAO = new ProfessorDAO();
        professorDAO.salvarProfessor(p);
        AlunoDAO alunoDAO = new AlunoDAO();
        alunoDAO.salvarAluno(a);
       
        System.out.println("Aluno:" + "Matricula Aluno" + a.getMatAluno() + "Nome Aluno" + a.getNome());
        System.out.println("Professor" + "Matricula Professor" + p.getMatProfessor() + "Nome Professor" + p.getNome());
       
       

    }
    catch(Exception e)
     {
       e.printStackTrace();
     }

    }
}


The error

Code:
WARNING: SQL Error: 0, SQLState: 23502
04/10/2009 13:17:16 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ERRO: valor nulo na coluna "nome" viola a restrição não-nula
04/10/2009 13:17:16 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [introducaohibernate01.Professor]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
        at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at introducaohibernate01.ProfessorDAO.salvarProfessor(ProfessorDAO.java:20)
        at introducaohibernate01.Main.main(Main.java:42)
Caused by: org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "nome" viola a restrição não-nula
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:304)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2250)
        ... 12 more
org.hibernate.exception.ConstraintViolationException: could not insert: [introducaohibernate01.Professor]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
        at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at introducaohibernate01.ProfessorDAO.salvarProfessor(ProfessorDAO.java:20)
        at introducaohibernate01.Main.main(Main.java:42)
Caused by: org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "nome" viola a restrição não-nula
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:304)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2250)
        ... 12 more
CONSTRUÍDO COM SUCESSO (tempo total: 2 segundos)


This SEVERE: ERROR: null value in column "name" violates not-null constraint

I put in Main Class
Code:
p.setNome("Astor Piazzola");
a.setNome("Renata Carvalho");



Anybody know where this error?


Top
 Profile  
 
 Post subject: [solved]org.hibernate.AnnotationException: Unknown Id.genera
PostPosted: Sun Oct 04, 2009 5:38 pm 
Beginner
Beginner

Joined: Sat Oct 03, 2009 1:51 pm
Posts: 26
I did, configure Hibernate to create the database, he created three tables, the table student and teacher have their own attributes, is different from what I had created that was using inherits on query postgre.

Image

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.