Entity Bean With Compound Key : Entity Bean « EJB3 « Java Tutorial






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");
    
  }
}

File: EmployeeServiceLocal.java

import javax.ejb.Local;
import javax.jws.WebParam;

@Local
public interface EmployeeServiceLocal {
  public void doAction();


}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;


@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

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;
  }
}

File: Main.java

import javax.ejb.EJB;
import javax.naming.InitialContext;


public class Main {

  public static void main(String[] a) throws Exception {

    EmployeeServiceRemote service = null;

    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");


    service.doAction();
    


  }

}

File: jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
  Download:  EJB-EntityBeanWithCompoundKey.zip( 4,488 k)








27.12.Entity Bean
27.12.1.Find Entity With Compound Key
27.12.2.Entity Bean With Generated ID
27.12.3.Entity Bean With Compound Key
27.12.4.Retrieve Entity Bean