Persistir em duas tabelas

2 respostas
Cristofe

Olá Pessoal,

Estou com problemas ao tentar persistir em duas tabelas do banco de dados.
Minha classe Entity é:

package 
@Entity
@Table(name = "usuario")
@SecondaryTable(name="telefone",pkJoinColumns={@PrimaryKeyJoinColumn(name="id")})

.......

    @Column(table="telefone",name="celular")
    private Integer telefone_celular;

    @Column(table="telefone",name="residencial")
    private Integer telefone_residencial;

    public Integer getTelefone_celular() {
        return telefone_celular;
    }

    public void setTelefone_celular(Integer telefone_celular) {
        this.telefone_celular = telefone_celular;
    }

    public Integer getTelefone_residencial() {
        return telefone_residencial;
    }

    public void setTelefone_residencial(Integer telefone_residencial) {
        this.telefone_residencial = telefone_residencial;
    }

.....
Os log sdizem que o relacionamento telefone nao existe. Coloco o nome da tabela na instrucao @SecondaryTable(name="telefone".
.....

Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Binding entity from annotated class: br.ifrr.entity.Usuario
Binding Named query: Usuario.findAll => SELECT u FROM Usuario u
Binding Named query: Usuario.findByStatus => SELECT u FROM Usuario u WHERE u.status = :status
Binding Named query: Usuario.findByPermissao => SELECT u FROM Usuario u WHERE u.permissao = :permissao
Binding Named query: Usuario.findById => SELECT u FROM Usuario u WHERE u.id = :id
Binding Named query: Usuario.findByNome => SELECT u FROM Usuario u WHERE u.nome = :nome
Binding Named query: Usuario.findByUsuario => SELECT u FROM Usuario u WHERE u.usuario = :usuario
Binding Named query: Usuario.findByPasswd => SELECT u FROM Usuario u WHERE u.passwd = :passwd
Binding Named query: Usuario.findByEmail => SELECT u FROM Usuario u WHERE u.email = :email
Bind entity br.ifrr.entity.Usuario on table usuario
Adding secondary table to entity br.ifrr.entity.Usuario -> telefone
Hibernate Validator not found: ignoring
Using Hibernate built-in connection pool (not for production use!)
Hibernate connection pool size: 20
autocommit mode: false
using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/aluno
connection properties: {user=postgres, password=****}
RDBMS: PostgreSQL, version: 9.0.3
JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.3 JDBC3 with SSL (build 603)
Using dialect: org.hibernate.dialect.PostgreSQLDialect
Using default transaction strategy (direct JDBC transactions)
No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Automatic flush during beforeCompletion(): disabled
Automatic session close at end of transaction: disabled
JDBC batch size: 15
JDBC batch updates for versioned data: disabled
Scrollable result sets: enabled
JDBC3 getGeneratedKeys(): disabled
Connection release mode: auto
Default batch fetch size: 1
Generate SQL with comments: disabled
Order SQL updates by primary key: disabled
Order SQL inserts for batching: disabled
Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Using ASTQueryTranslatorFactory
Query language substitutions: {}
JPA-QL strict compliance: disabled
Second-level cache: enabled
Query cache: disabled
Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
Optimize cache for minimal puts: disabled
Structured second-level cache entries: disabled
Statistics: disabled
Deleted entity synthetic identifier rollback: disabled
Default entity-mode: pojo
Named query checking : enabled
building session factory
Not binding factory to JNDI, no JNDI name configured
SQL Error: 0, SQLState: 42P01
ERROR: relation "telefone" does not exist
Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert: [br.ifrr.entity.Usuario]
        .....

        Caused by: org.postgresql.util.PSQLException: ERROR: relation "telefone" does not exist
        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:2278)
        ... 44 more
[salvar] Nao foi possivel Salva o usuario

Ele diz que o relacionamento telefone nao existe. porém estou utilizando a chave id da tabela aluno para gravar no id_aluno da tabela telefone.

segue as tabelas do banco:

CREATE TABLE usuario
(
  status integer,
  nome character(40),
  usuario character(10),
  passwd integer,
  email character(40),
  "dataCriacao" date,
  id integer NOT NULL,
  permissao integer,
  CONSTRAINT id_usuario PRIMARY KEY (id),
  CONSTRAINT login_usuario UNIQUE (usuario)
)
WITH (
  OIDS=FALSE,
  autovacuum_enabled=true
);
ALTER TABLE usuario OWNER TO postgres;
GRANT ALL ON TABLE usuario TO public;
GRANT ALL ON TABLE usuario TO postgres;

Tabela telefone

[/code]
CREATE TABLE "TELEFONE"
(
id integer NOT NULL,
residencial integer,
celular integer,
id_aluno integer,
CONSTRAINT pk_telefone PRIMARY KEY (id),
CONSTRAINT fk_telefone FOREIGN KEY (id_aluno)
REFERENCES "ALUNO" (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE "TELEFONE" OWNER TO postgres;
GRANT ALL ON TABLE "TELEFONE" TO postgres;
GRANT ALL ON TABLE "TELEFONE" TO public;

-- Index: fki_telefone

-- DROP INDEX fki_telefone;

CREATE INDEX fki_telefone
ON "TELEFONE"
USING btree
(id_aluno);
[code]

Alguem pode me ajudar?

2 Respostas

Hebert_Coelho

Cara, seu banco não é case sensitive não? Você criou TELEFONE mas na classe colocou telefone.

Será que isso? :roll:

Cristofe

Cara alterei a tabela para telefone em minúsculo e mesmo assim continua. Estou utilizando um MAC OS Snow Leopard para desenvolvimento. Acredito que não é isso.

Criado 2 de maio de 2012
Ultima resposta 2 de mai. de 2012
Respostas 2
Participantes 2