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 ClassCode:
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 queryCode:
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