Setup Entity JPA Development Environment : JPA « EJB3 « Java Tutorial






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

}

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.setFirstName("Joe");
    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: 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

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();
    


  }

}
  Download:  EJB-SetupEntityJPADevEnv.zip( 4,487 k)








27.9.JPA
27.9.1.Setup Entity JPA Development Environment