-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query return null
PostPosted: Fri Jul 22, 2005 9:16 am 
Newbie

Joined: Thu Feb 24, 2005 8:37 am
Posts: 8
Hi I trying to make a query with annotations but the value returns always null, I was reading javadoc documentation and the tutorials but I did'nt have any success, I would like some help with this problem:

Connection Class:

Code:

package com.tsw.config;

import org.hibernate.*;
import org.hibernate.cfg.*;
import com.tsw.persistente.*;

public class Configura {

private static final SessionFactory sessionFactory;

    static {
       
        try {

            sessionFactory = new AnnotationConfiguration()

            .addPackage("com.tsw.persistente")
            .addAnnotatedClass(Funcionario.class)
            .buildSessionFactory();
       
        } catch (Throwable ex) {
            // Log exception!
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }
   
}



Persistent Class

Code:

package com.tsw.persistente;

import java.io.Serializable;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.*;

@Entity(name="Funcionario")
public class Funcionario implements Serializable {
   
    private int id_funcionario;
    private String nome_funcionario;
    private String email_funcionario;
    private String senha_funcionario;
   
    @Id
    public int getId_funcionario() {
        return id_funcionario;
    }

    public void setId_funcionario(int id_funcionario) {
        this.id_funcionario = id_funcionario;
    }

    public String getNome_funcionario() {
        return nome_funcionario;
    }

    public void setNome_funcionario(String nome_funcionario) {
        this.nome_funcionario = nome_funcionario;
    }

    public String getEmail_funcionario() {
        return email_funcionario;
    }

    public void setEmail_funcionario(String email_funcionario) {
        this.email_funcionario = email_funcionario;
    }

    public String getSenha_funcionario() {
        return senha_funcionario;
    }

    public void setSenha_funcionario(String senha_funcionario) {
        this.senha_funcionario = senha_funcionario;
    }
   
}




Classe that make a query

Code:
package com.tsw.persistente;

import java.util.List;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.StaleStateException;
import org.hibernate.Transaction;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import com.tsw.config.*;

public class Teste {
   
    public Teste() {
       
                Session s = new Configura().getSession();
                Transaction tx = s.beginTransaction();
                Funcionario func = new Funcionario();

                func = (Funcionario) s.get(Funcionario.class, func.getId_funcionario());
               
                if (func == null) {
                   
                    System.out.println("Return Null!");
               
                } else {
               
                    System.out.println("Teste " + func.getSenha_funcionario());
                }
       }
   
    public static void main(String[] args) {
           
            new Teste();
    }
}



My little table in postgresql 8.0 :

Code:

Create table Funcionario
(
        ID_FUNCIONARIO Serial NOT NULL,
        NOME_FUNCIONARIO Varchar NOT NULL,
        EMAIL_FUNCIONARIO Varchar NOT NULL,
        SENHA_FUNCIONARIO Varchar NOT NULL,
primary key (ID_FUNCIONARIO)
) With Oids;




Thanks for all helpful efforts

_________________
---
-------------------------------------------------------------------------
Volenti Nihil Dificille


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Dude, read your code :-)
Code:
Funcionario func = new Funcionario(); //ie and id=0
func = (Funcionario) s.get(Funcionario.class, func.getId_funcionario());

So there is no element with id 0 in your table.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Query return null
PostPosted: Fri Jul 22, 2005 2:00 pm 
Newbie

Joined: Thu Feb 24, 2005 8:37 am
Posts: 8
Hi emmanuel, I was really lost, with a little big detail, thanks for help me You solved my problem!

best regards

emmanuel wrote:
Dude, read your code :-)
Code:
Funcionario func = new Funcionario(); //ie and id=0
func = (Funcionario) s.get(Funcionario.class, func.getId_funcionario());

So there is no element with id 0 in your table.

_________________
---
-------------------------------------------------------------------------
Volenti Nihil Dificille


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.