Java + Hibernate

7 respostas
P
Alguem pode me ajudar nao sei porque esta dando esse erro:
C:\jdevstudio1013\jdk\bin\javaw.exe -ojvm -classpath C:\jdevstudio1013\jdev\mywork\Universidade\Universidade\classes;C:\jdevstudio1013\jlib\postgresql-8[1].1-405.jdbc2ee.jar;C:\jdevstudio1013\jlib\hibernate-3.2\hibernate3.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ant-1.6.5.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ant-antlr-1.6.5.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ant-junit-1.6.5.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ant-launcher-1.6.5.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ant-swing-1.6.5.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\antlr-2.7.6.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\asm-attrs.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\asm.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\c3p0-0.9.0.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\cglib-2.1.3.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\cleanimports.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\commons-collections-2.1.1.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\commons-logging-1.0.4.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\concurrent-1.3.2.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\connector.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\dom4j-1.6.1.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\ehcache-1.2.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jaas.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jacc-1_0-fr.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\javassist.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jaxen-1.1-beta-7.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jboss-cache.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jboss-common.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jboss-jmx.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jboss-system.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jdbc2_0-stdext.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jgroups-2.2.8.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\jta.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\junit-3.8.1.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\log4j-1.2.11.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\oscache-2.1.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\proxool-0.8.3.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\swarmcache-1.0rc2.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\syndiag2.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\versioncheck.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\xerces-2.6.2.jar;C:\jdevstudio1013\jlib\hibernate-3.2\lib\xml-apis.jar -Dhttp.proxyHost=192.168.0.1 -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=201.34.35.231|201.2.63.12|192.*|*.tj.ro.gov.br|*.tjro.net|localhost|127.0.0.1 -Dhttps.proxyHost=192.168.0.1 -Dhttps.proxyPort=80 -Dhttps.nonProxyHosts=201.34.35.231|201.2.63.12|192.*|*.tj.ro.gov.br|*.tjro.net|localhost|127.0.0.1 Teste
16:11:01,531  WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/C:/jdevstudio1013/jlib/hibernate-3.2/lib/ehcache-1.2.jar!/ehcache-failsafe.xml
16:11:02,390 ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class: Pessoa, getter method of property: id
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Pessoa.id
	at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
	at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
	at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
	at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:114)
	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:418)
	at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:90)
	at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:58)
	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:223)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
	at HibernateUtility.<clinit>(HibernateUtility.java:10)
	at Teste.main(Teste.java:7)
Caused by: java.lang.IllegalArgumentException: this is not an instance of the method's owning class
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
	... 10 more
Exception in thread main
java.lang.NullPointerException
	at HibernateUtility.getSession(HibernateUtility.java:18)
	at Teste.main(Teste.java:7)
Process exited with exit code 1.
classe main Teste.java
//Arquivo Teste.java
import org.hibernate.*;


public class Teste {
    public static void main(String[] args) {
        Session sessao = HibernateUtility.getSession(); //Abrindo uma sessão
        Transaction transaction = 
            sessao.beginTransaction(); //Iniciando uma transação
        Curso curso = new Curso(); //Instanciando um objeto transiente
        curso.setNomeC("Desenvolvimento de Software"); //Preenchendo as propriedades do objeto
        curso.setDescricaoC("Curso só pra programadores");
        sessao.save(curso); //Transformando o objeto transiente em um objeto
        //persistente no banco de dados
        transaction.commit(); //Finalizando a transação
        sessao.close(); //Fechando a sessão
    }
}
classe Pessoa.java
public class Pessoa {
    private Integer id;
    private String nomeP;
    private String email;
    private String telefone;

    public Pessoa() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNomeP() {
        return nomeP;
    }

    public void setNomeP(String nomeP) {
        this.nomeP = nomeP;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
}
Classe Aluno.java
public class Aluno {
    private String matricula;
    private Pessoa pessoa;

    public Aluno() {
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setFita(Pessoa pessoa) {
        this.pessoa = pessoa;
    }
}
Classe Professor.java
public class Professor {
    private String titulo;
    private Pessoa pessoa;

    public Professor() {
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setFita(Pessoa pessoa) {
        this.pessoa = pessoa;
    }
}
Classe Endereco.java
public class Endereco {
    private Integer pessoa_id;
    private String rua;
    private Integer numero;
    private String bairro;
    private String estado;
    private String complemento;
    private String cep;
    private String cidade;
    private Pessoa pessoa;

    public Endereco() {
    }

    public Integer getPessoa_id() {
        return pessoa_id;
    }

    public void setPessoa_id(Integer pessoa_id) {
        this.pessoa_id = pessoa_id;
    }

    public String getRua() {
        return rua;
    }

    public void setRua(String rua) {
        this.rua = rua;
    }

    public Integer getNumero() {
        return numero;
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String getComplemento() {
        return complemento;
    }

    public void setComplemento(String complemento) {
        this.complemento = complemento;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setPessoa(Pessoa pessoa) {
        this.pessoa = pessoa;
    }
}

TENHO MAIS CLASSE MAS ACHO QUE ESSA QUE O PROBLEMA E COM RELACAO A ESSAS QUE SE RELACIONAM.

7 Respostas

plentz

Pelo contrário, tudo indica que o problema está nessa sua outra classe.

Coloque o código da HibernateUtility.

P

ok! desde já obrigado!
Ai esta o código!!!

//Arquivo HibernateUtility.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtility {
    private static SessionFactory factory;
    static {
        try {
            factory = new Configuration().configure().buildSessionFactory();
        } catch (Exception e) {
            e.printStackTrace();
            factory = null;
        }
    }

    public static Session getSession() {
        return factory.openSession();
    }
}
P

Posta também o arquivo de configuração do Hibernate, hibernate.properties (ou hibernate.cfg.xml).
Posta também o arquivo de mapeamento da classe Pessoa

P
ok.... estou postando hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/BDUniversidade</property>
        <property name="connection.username">paulo</property>
        <property name="connection.password">paulo</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">10</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="Curso.hbm.xml"/>
        <mapping resource="Disciplina.hbm.xml"/>
        <mapping resource="Turma.hbm.xml"/>
        <mapping resource="Pessoa.hbm.xml"/>
        <mapping resource="Aluno.hbm.xml"/>
        <mapping resource="Professor.hbm.xml"/>
        <mapping resource="Endereco.hbm.xml"/>
        
    </session-factory>
    
</hibernate-configuration>
pessoa.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
        PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="Pessoa">

        <!-- Identificador da classe -->
        <id name="id">
            <generator class="increment"/>
        </id>
    
        <!-- Propriedades da classe -->
        <property name="nomeP"/>
        <property name="telefone"/>
        <property name="email"/>
    
        <!-- Relacionamento da classe -->
        <one-to-one name="endereco"  class="Endereco"  cascade="save-update"/>
    
    </class>
    
</hibernate-mapping>
professor.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <joined-subclass name="Professor" extends="Pessoa">
        <key column="Pessoa_id"/>
        <property name="titulo"/>        
        <set name="turmas"
            inverse="true">
            <key column="Pessoa_Professor_id"/>
            <one-to-many class="Turma"/>
        </set>
    </joined-subclass>
</hibernate-mapping>
aluno.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
        PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
    <joined-subclass name="Aluno" extends="Pessoa">
        <key column="Pessoa_id"/>
        <property name="matricula"/>
        <set name="turmas"
            table="Turma_has_Aluno"
            inverse="true">
            <key column="Turma_id"/>
            <many-to-many class="Turma"/>
        </set>
        
    </joined-subclass>
</hibernate-mapping>
endereco.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
        PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="Endereco">
        <id name="id"
            column="Pessoa_id">
            <generator class="foreign">
                <param name="property">pessoa</param>
            </generator>
        </id>
        <property name="bairro"/>
        <property name="cidade"/>
        <property name="complemento"/>
        <property name="estado"/>
        <property name="numero"/>
        <property name="rua"/>
        <property name="cep"/>
        <one-to-one
            name="pessoa"
            class="Pessoa"
            constrained="true"/>
    </class>
</hibernate-mapping>
P

Por Favor, Alguém pode me ajudar!!??

Paulo_Silveira

Aluno e Professor nao estao estendendo de Pessoa

P

Obrigado! Resolvi o problema.

Criado 14 de outubro de 2006
Ultima resposta 19 de out. de 2006
Respostas 7
Participantes 4