Mapeamento - Hibernate

3 respostas
V

Boa Tarde…

Estou com uma dúvida em algo inédito comigo no Hibernate… Tenho uma tabela com 230 campos, dessa tabela, eu preciso somente consultar tres campos atravéz da chave primária…
Eu preciso mapear TODOS os campos da tabela na POJO e no XML, ou somente os que eu vou consultar ?!
e quanto a chave primária, eu preciso declarar na tag “ID” ?

Desde já agradeço pela atenção !

[ ]'s !!!

3 Respostas

T

normalmente vc vai mapear os campos que se tornarao atributos de uma / varias classes…
os campos que vc nao mapear, nao serao lidos do bd…

quanto ao id, se a chave eh composta, se eu nao me engano a tag eh composite-id, mas de uma olhada na documentacao do hibernate

V

Valew Takeshi !
o mapeamento funcionou…
estarei verificando quanto ao ID…

[ ]'s

lsouzabr

Pessoal, seguinte…

Estou estudando mapeamentos.

No caso, eu gostaria de entender melhor o mapeamento @onetomany, estou fazendo aqui, mas o toplink me retorna erros.
A cena é a seguinte:

Um estado têm vários municípios.
Acredito que isso seja unidirecional.

vou colocar o código das classes:

Estados[color=blue] [/color]

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unidirecional.onetomany;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

/**
 *
 * @author leonamsouza
 */
@Entity
@SequenceGenerator(name="$IdEstados",sequenceName="$IdEstados")
public class Estados implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "$IdEstados")
    private Integer $IdEstados;
    private String $Nome;

   @OneToMany(cascade=CascadeType.PERSIST)
   private Set municipios = new HashSet();

   public Estados() {
   }

   public Set getMunicipios() {
       return municipios;
   }

   public void setMunicipios(Municipios municipios) {
       this.municipios.add(municipios);
   }

    
    public Integer get$IdEstados() {
        return $IdEstados;
    }

    public void set$IdEstados(Integer $IdEstados) {
        this.$IdEstados = $IdEstados;
    }

    public String get$Nome() {
        return $Nome;
    }

    public void set$Nome(String $Nome) {
        this.$Nome = $Nome;
    }
    
    

}

Municípios[color=blue] [/color]

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unidirecional.onetomany;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

/**
 *
 * @author leonamsouza
 */
@Entity
@SequenceGenerator(name="$IdMunicipios",sequenceName="$IdMunicipios")
public class Municipios implements Serializable
{
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "$IdMunicipios")
    private Integer $IdMunicipios;
    private String Nome;
    
    public Integer get$IdMunicipios() {
        return $IdMunicipios;
    }

    public void set$IdMunicipios(Integer $IdMunicipios) {
        this.$IdMunicipios = $IdMunicipios;
    }

    public String getNome() {
        return Nome;
    }

    public void setNome(String Nome) {
        this.Nome = Nome;
    }
    
    public Municipios(){}
    
}

Mensagens do toplink[color=blue] [/color]

init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named UP: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory: 
oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Local Exception Stack: 
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@a9c85c
Internal Exception: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [UP] failed.
Internal Exception: Exception [TOPLINK-7214] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The target entity of the relationship attribute [municipios] on the class [class unidirecional.onetomany.Estados] cannot be determined.  When not using generics, ensure the target entity is defined on the relationship mapping.
        at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
        at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at dao.GenericDAO.<init>(GenericDAO.java:19)
        at bidirecional.onotoone.Teste.main(Teste.java:28)
Caused by: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [UP] failed.
Internal Exception: Exception [TOPLINK-7214] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The target entity of the relationship attribute [municipios] on the class [class unidirecional.onetomany.Estados] cannot be determined.  When not using generics, ensure the target entity is defined on the relationship mapping.
        at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:643)
        at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.callPredeploy(JavaSECMPInitializer.java:171)
        at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:239)
        at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:255)
        at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:155)
        ... 4 more
Caused by: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [UP] failed.
Internal Exception: Exception [TOPLINK-7214] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The target entity of the relationship attribute [municipios] on the class [class unidirecional.onetomany.Estados] cannot be determined.  When not using generics, ensure the target entity is defined on the relationship mapping.
        at oracle.toplink.essentials.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:228)
        ... 9 more
Caused by: Exception [TOPLINK-7214] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The target entity of the relationship attribute [municipios] on the class [class unidirecional.onetomany.Estados] cannot be determined.  When not using generics, ensure the target entity is defined on the relationship mapping.
        at oracle.toplink.essentials.exceptions.ValidationException.unableToDetermineTargetEntity(ValidationException.java:1758)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataValidator.throwUnableToDetermineTargetEntity(MetadataValidator.java:408)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.CollectionAccessor.getReferenceClass(CollectionAccessor.java:181)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.RelationshipAccessor.processRelationship(RelationshipAccessor.java:287)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProject.processRelationshipDescriptors(MetadataProject.java:579)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProject.process(MetadataProject.java:512)
        at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.processAnnotations(MetadataProcessor.java:246)
        at oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:370)
        at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:607)
        ... 8 more


 The following providers:
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.

        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at dao.GenericDAO.<init>(GenericDAO.java:19)
        at bidirecional.onotoone.Teste.main(Teste.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Neste projetos tenho vários outros exemplos, todos funcionando perfeitamente.
Porém quando adiciono estas duas classes na unidade de persistência, tudo para de funcionar.

Desculpem-me se postei a dúvida no lugar errado.

Criado 20 de abril de 2006
Ultima resposta 14 de out. de 2008
Respostas 3
Participantes 3