Criar tabelas c/ Hibernate Annotations - Problema c/ chave estrangeira

1 resposta
heitorr

CriarTabelas.java
package br.cefetmt.drec.model.util;

import br.cefetmt.drec.model.bean.*;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CriarTabelas {

public static void main(String[] args) {

        // Cria uma configuração para a classe Produto
        List classes = new ArrayList();

        classes.add(Municipio.class);
        classes.add(UF.class);
        
        for (int x = 0; x < classes.size(); x++) {
            Object c = classes.get(x);
            AnnotationConfiguration cfg = new AnnotationConfiguration();
            cfg.addAnnotatedClass((Class)c);
            new SchemaExport(cfg).create(true, true);
            cfg = null;
        }
    }

}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> org.postgresql.Driver assiralJSF jdbc:postgresql://localhost/drec postgres org.hibernate.dialect.PostgreSQLDialect create
<mapping class="br.cefetmt.drec.model.bean.UF"/>
    <mapping class="br.cefetmt.drec.model.bean.Municipio"/>
</session-factory>

UF.java
package br.cefetmt.drec.model.bean;

import java.util.List;
import javax.persistence.*;

@Entity

@SequenceGenerator(name=SEQ_UF,

sequenceName=SEQ_UF,

allocationSize = 1)

public class UF {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="SEQ_UF")
private long id;
private String nome;
private String sigla;

@OneToMany(mappedBy="uf")
private List <Municipio> municipios;

// contrutores, getters e setters
}

Municipio.java
package br.cefetmt.drec.model.bean;

import javax.persistence.*;

@Entity

@SequenceGenerator(name=SEQ_MUNICIPIO,

sequenceName=SEQ_MUNICIPIO,

allocationSize = 1)

public class Municipio {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="SEQ_MUNICIPIO")
private long id;
private String nome;

@ManyToOne
@JoinColumn(name="uf_id")
private UF uf;

// contrutores, getters e setters
}

Erro:

(annotations.Version                 15  ) Hibernate Annotations <a href="http://3.2.0.GA">3.2.0.GA</a>

(cfg.Environment                     500 ) Hibernate 3.2.0

(cfg.Environment                     518 ) loaded properties from resource hibernate.properties: {hibernate.connection.username=postgres, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect, hibernate.connection.url=jdbc:postgresql://localhost/drec, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=org.postgresql.Driver}

(cfg.Environment                     667 ) Bytecode provider name : cglib

(cfg.Environment                     584 ) using JDK 1.4 java.sql.Timestamp handling

(dialect.Dialect                     141 ) Using dialect: org.hibernate.dialect.PostgreSQLDialect

(cfg.AnnotationBinder                387 ) Binding entity from annotated class: br.cefetmt.drec.model.bean.Municipio

(annotations.EntityBinder            340 ) Bind entity br.cefetmt.drec.model.bean.Municipio on table Municipio

Exception in thread main

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on br.cefetmt.drec.model.bean.Municipio.uf references an unknown entity: br.cefetmt.drec.model.bean.UF

at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)

at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)

at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)

at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:687)

at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:93)

at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:62)

at br.cefetmt.drec.model.util.CriarTabelas.main(CriarTabelas.java:40)

Process exited with exit code 1.

1 Resposta

thigo.san

Estou com o mesmo problema, voce conseguiu resolver?

Criado 5 de novembro de 2006
Ultima resposta 25 de jan. de 2009
Respostas 1
Participantes 2