Java Tutorial/JPA/Clob Blob

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Java Byte Array Lob Mapped To Long VarBinary

File: Main.java



import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();
  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();
    
    
    Student student = new Student();
    student.setId(1);
    student.setName("Joe");
    student.setPicture("pic".getBytes());
    
    em.persist(student);
    em.flush();
    em.getTransaction().rumit();
    
    Query query = em.createQuery("SELECT e FROM Student e");
    List<Student> list = (List<Student>) query.getResultList();
    System.out.println(list);
    
    
    em.close();
    emf.close();
    
    Helper.checkData();
  }
}





Map Serializable Entity To Lob

File: Customer.java



import javax.persistence.*;
import java.util.Date;
@Entity
public class Customer implements java.io.Serializable 
{
   private int id;
   private JPEG picture;
   
   @Id
   @GeneratedValue
   public int getId()
   {
      return id;
   }
   public void setId(int pk) 
   {
      id = pk;
   }

   @Lob @Basic(fetch=FetchType.LAZY)
   public JPEG getPicture() { return picture; }
   public void setPicture(JPEG jpeg) { picture = jpeg; }
}





Mark Abstract Entity With Mapped Superclass

File: Employee.java



import java.util.Date;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "EMP")
@Inheritance
@DiscriminatorColumn(name = "EMP_TYPE")
public abstract class Employee {
  @Id
  private int id;
  private String name;
  @Temporal(TemporalType.DATE)
  @Column(name = "S_DATE")
  private Date startDate;
  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 Date getStartDate() {
    return startDate;
  }
  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName();
  }
}
@MappedSuperclass
abstract class AbstractEmployee extends Employee {
  private int vacation;
  public int getVacation() {
    return vacation;
  }
  public void setVacation(int vacation) {
    this.vacation = vacation;
  }
}
@Entity
@DiscriminatorValue("FTEmp")
class FullTimeEmployee extends AbstractEmployee {
  private long salary;
  private long pension;
  public long getPension() {
    return pension;
  }
  public void setPension(long pension) {
    this.pension = pension;
  }
  public long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  public String toString() {
    return "FullTimeEmployee id: " + getId() + " name: " + getName();
  }
}
@Entity(name = "PTEmp")
@DiscriminatorValue("PTEmp")
class PartTimeEmployee extends AbstractEmployee {
  @Column(name = "H_RATE")
  private float hourlyRate;
  public float getHourlyRate() {
    return hourlyRate;
  }
  public void setHourlyRate(float hourlyRate) {
    this.hourlyRate = hourlyRate;
  }
  public String toString() {
    return "PartTimeEmployee id: " + getId() + " name: " + getName();
  }
}





Mark Byte Array Field As Lob

File: Student.java



import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;
@Entity
public class Student {
  @Id
  private int id = 0;
  private String name;
  @Basic(fetch = FetchType.LAZY)
  @Lob
  private byte[] picture;
  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 byte[] getPicture() {
    return picture;
  }
  public void setPicture(byte[] picture) {
    this.picture = picture;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}





Mark String As Lob

File: Student.java



import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;
@Entity
public class Student {
  @Id
  private int id = 0;
  @Lob
  private String name;

  
  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 String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}