ตัวอย่างการ query โดยใช้ Hibernate

http://bit.ly/hibernateQuery

ตัวอย่างการ query โดยใช้ hibernate

คลาสที่อยู่

package model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Address {

  @Id
  @GeneratedValue
  private int id;
  @Column
  private String province;
  @Column
  private int zip;

  public int getId() {
    return id;
  }

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

  public String getProvince() {
    return province;
  }

  public void setProvince(String province) {
    this.province = province;
  }

  public int getZip() {
    return zip;
  }

  public void setZip(int zip) {
    this.zip = zip;
  }

  @Override
  public String toString() {
    return "Address{" + "id=" + id + ", province=" + province + ", zip=" + zip + '}';
  }
}

คลาสพนักงาน ดูเมธอด isUserPasswordCorrect และ all

package th.co.truecorp;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.Query;
import org.hibernate.Session;

@Entity
public class Employee {

  @Id
  @GeneratedValue
  private int id;
  @Column
  private String name;
  @Column
  private String password;
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private List<Address> addresses = new ArrayList();

  public static boolean isUserPasswordCorrect(String usr, String pwd) {
    Session s = HibernateUtil.getSessionFactory().openSession();
    Query q = s.createQuery("from Employee where name = :pName and password = :pPassword");
    q.setParameter("pName", usr);
    q.setParameter("pPassword", pwd);
    int n = q.list().size();
    s.close();
    return n > 0;
  }

  public static List<Employee> all() {
    Session s = HibernateUtil.getSessionFactory().openSession();
    List<Employee> list = (List<Employee>) s.createQuery("from Employee").list();
    s.close();
    return list;
  }

  @Override
  public String toString() {
    return "Employee{" + "id=" + id + ", name=" + name + ", addresses=" + addresses + '}';
  }

  public int getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<Address> getAddresses() {
    return addresses;
  }

  public void setAddresses(List<Address> addresses) {
    this.addresses = addresses;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

ทดสอบด้วยคลาสนี้

package model;

public class Test {

  public static void main(String[] args) {
    System.out.println(Employee.isUserPasswordCorrect("Somchai", "Somchai12"));
    System.out.println(Employee.isUserPasswordCorrect("Somchai", "Somchai123"));
    System.out.println(Employee.all());
  }
}

ดูสไลด์ Hibernate ได้ที่ http://bit.ly/openslide

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s