Java Tutorial/EJB3/Entity Bean — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:02, 1 июня 2010

Entity Bean With Compound Key

File: EmployeeBean.java



import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService") private EntityManager manager;
  public void doAction(){
    Customer cust = new Customer();
    cust.setLastName("Bond");
    cust.setSsn(123456789L);
    manager.persist(cust);
    
    System.out.println("Saved");
    
  }
}





Entity Bean With Generated ID

File: Customer.java



import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "CUSTOMER_TABLE")
public class Customer implements java.io.Serializable {
  private int id;
  private String lastName;
  private String firstName;
  private Date timeCreated = new Date();
  @Id
  @GeneratedValue
  @Column(name = "CUST_ID")
  public int getId() {
    return id;
  }
  public void setId(int pk) {
    id = pk;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  @Temporal(TemporalType.TIME)
  public Date getTimeCreated() {
    return timeCreated;
  }
  public void setTimeCreated(Date time) {
    timeCreated = time;
  }
}





Find Entity With Compound Key

File: Customer.java



import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(CustomerPK.class)
public class Customer implements java.io.Serializable {
  private String firstName;
  private String lastName;
  private long ssn;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  @Id
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  @Id
  public long getSsn() {
    return ssn;
  }
  public void setSsn(long ssn) {
    this.ssn = ssn;
  }
}
class CustomerPK implements java.io.Serializable {
  private String lastName;
  private long ssn;
  public CustomerPK() {
  }
  public CustomerPK(String lastName, long ssn) {
    this.lastName = lastName;
    this.ssn = ssn;
  }
  public String getLastName() {
    return this.lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public long getSsn() {
    return ssn;
  }
  public void setSsn(long ssn) {
    this.ssn = ssn;
  }
  public boolean equals(Object obj) {
    if (obj == this)
      return true;
    if (!(obj instanceof CustomerPK))
      return false;
    CustomerPK pk = (CustomerPK) obj;
    if (!lastName.equals(pk.lastName))
      return false;
    if (ssn != pk.ssn)
      return false;
    return true;
  }
  public int hashCode() {
    return lastName.hashCode() + (int) ssn;
  }
}





Retrieve Entity Bean

File: Customer.java



import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "CUSTOMER_TABLE")
public class Customer implements java.io.Serializable {
  private int id;
  private String lastName;
  private String firstName;
  private Date timeCreated = new Date();
  @Id
  @GeneratedValue
  @Column(name = "CUST_ID")
  public int getId() {
    return id;
  }
  public void setId(int pk) {
    id = pk;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  @Temporal(TemporalType.TIME)
  public Date getTimeCreated() {
    return timeCreated;
  }
  public void setTimeCreated(Date time) {
    timeCreated = time;
  }
}