Java/EJB3/Persistence

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

Embeddable Entity

File: EmployeeService.java

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public void doAction(){
    Employee cust = new Employee();
    cust.setFirstName("B");
    cust.setLastName("B");
    Address address = new Address();
    address.setStreet("Street");
    address.setCity("Boston");
    address.setState("MA");
    cust.setAddress(address);
    
    em.persist(cust);
    em.flush();
    System.out.println("saved");
    
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
}

File: Address.java

import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class Address implements java.io.Serializable {
   private String street;
   private String city;
   private String state;
   @Column(name="STREET")
   public String getStreet() { return street; }
   public void setStreet(String street) { this.street = street; }
   @Column(name="CITY")
   public String getCity() { return city; }
   public void setCity(String city) { this.city = city; }
   @Column(name="STATE")
   public String getState() { return state; }
   public void setState(String state) { this.state = state; }
}

File: Employee.java
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Employee implements java.io.Serializable {
  private long id;
  private String firstName;
  private String lastName;
  private Address address;
  @Id
  @GeneratedValue
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
  @Embedded
  @AttributeOverrides( { @AttributeOverride(name = "street", column = @Column(name = "STREET")),
      @AttributeOverride(name = "city", column = @Column(name = "CITY")),
      @AttributeOverride(name = "state", column = @Column(name = "STATE")) })
  public Address getAddress() {
    return address;
  }
  public void setAddress(Address address) {
    this.address = address;
  }
}

File: Main.java
import java.util.Collection;
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("EmployeeService/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





Entity With Date

File: Employee.java
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  private CustomerType customerType;
  private Date timeCreated = new Date();
  private MyImage picture;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  @Enumerated(EnumType.STRING)
  public CustomerType getCustomerType() { return customerType; }
  public void setCustomerType(CustomerType type) { customerType = type; }
  @Temporal(TemporalType.TIME)
  public Date getTimeCreated() { return timeCreated; }
  public void setTimeCreated(Date time) { timeCreated = time; }
  @Lob @Basic(fetch=FetchType.LAZY)
  public MyImage getPicture() { return picture; }
  public void setPicture(MyImage jpeg) { picture = jpeg; }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(Employee emp) {
    em.persist(emp);
    em.flush();
    return emp;
  }
  public Employee createEmployee(int id, String name, long salary) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    em.flush();
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
  }
  @Remove
  public void remove()
  {
    System.out.println("removed");
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
    public Employee createEmployee(int id, String name, long salary) ;
    public Employee createEmployee(Employee emp) ;
    public void removeEmployee(int id);
    public Employee raiseEmployeeSalary(int id, long raise) ;
    public Employee findEmployee(int id);
    public Collection<Employee> findAllEmployees() ;
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
  public Employee createEmployee(int id, String name, long salary) ;
  public Employee createEmployee(Employee emp) ;
  public void removeEmployee(int id);
  public Employee raiseEmployeeSalary(int id, long raise) ;
  public Employee findEmployee(int id);
  public Collection<Employee> findAllEmployees() ;  
}

File: MyImage.java
public class MyImage implements java.io.Serializable
{
   public MyImage() {}
}

File: CustomerType.java

public enum CustomerType
{
   UNREGISTERED,
   REGISTERED,
   BIG_SPENDAH
}
   

File: Main.java
import java.util.Collection;
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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000);
    service.createEmployee(159, "AAA", 45000);
    
    Employee emp = new Employee();
    emp.setId(160);
    emp.setCustomerType(CustomerType.BIG_SPENDAH);
    MyImage oneUglyDude = new MyImage();
    emp.setPicture(oneUglyDude);
    
    service.createEmployee(emp);
    Employee emp1 = service.findEmployee(160);
    System.out.println(emp1);
    
    Collection<Employee> list = service.findAllEmployees();
    
    System.out.println(list);
  }
}

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





Flush Data In EJB

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  @PersistenceUnit(unitName="EmployeeService") private EntityManagerFactory factory;
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
    EntityManager createdManager = factory.createEntityManager();
    
    
    Employee emp = new Employee(2);
    emp.setName("name");
    emp.setSalary(200);
        
    createdManager.persist(emp);
    createdManager.flush();
    
    emp = em.find(Employee.class, 2);
    
    System.out.println(emp);
  }
}
    

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
}

File: EmployeeServiceRemote.java


import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  
  public void doAction();
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: Main.java
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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    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





Get List Of Employees From Ejb

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    em.flush();
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
    public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
    public void removeEmployee(int id);
    public Employee raiseEmployeeSalary(int id, long raise) ;
    public Employee findEmployee(int id);
    public Collection<Employee> findAllEmployees() ;
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  public void removeEmployee(int id);
  public Employee raiseEmployeeSalary(int id, long raise) ;
  public Employee findEmployee(int id);
  public Collection<Employee> findAllEmployees() ;  
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: Main.java
import java.util.Collection;
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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
    Employee emp = service.findEmployee(158);
    System.out.println(emp);
    
    Collection<Employee> list = service.findAllEmployees();
    
    System.out.println(list);
  }
}

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





Persistence Context Type: EXTENDED

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
@Stateful
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED)
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    em.flush();
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
  }
  @Remove
  public void remove()
  {
    System.out.println("removed");
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
    public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
    public void removeEmployee(int id);
    public Employee raiseEmployeeSalary(int id, long raise) ;
    public Employee findEmployee(int id);
    public Collection<Employee> findAllEmployees() ;
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  public void removeEmployee(int id);
  public Employee raiseEmployeeSalary(int id, long raise) ;
  public Employee findEmployee(int id);
  public Collection<Employee> findAllEmployees() ;  
}

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 java.util.Collection;
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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
    Employee emp = service.findEmployee(158);
    System.out.println(emp);
    
    Collection<Employee> list = service.findAllEmployees();
    
    System.out.println(list);
  }
}





Persistence Context Type: TRANSACTION

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
@Stateful
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.TRANSACTION)
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    em.flush();
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
    public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
    public void removeEmployee(int id);
    public Employee raiseEmployeeSalary(int id, long raise) ;
    public Employee findEmployee(int id);
    public Collection<Employee> findAllEmployees() ;
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  public void removeEmployee(int id);
  public Employee raiseEmployeeSalary(int id, long raise) ;
  public Employee findEmployee(int id);
  public Collection<Employee> findAllEmployees() ;  
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: Main.java
import java.util.Collection;
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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
    Employee emp = service.findEmployee(158);
    System.out.println(emp);
    
    Collection<Employee> list = service.findAllEmployees();
    
    System.out.println(list);
  }
}

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





Retrieve Data From Ejb

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    em.flush();
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
    public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
    public void removeEmployee(int id);
    public Employee raiseEmployeeSalary(int id, long raise) ;
    public Employee findEmployee(int id);
    public Collection<Employee> findAllEmployees() ;
}

File: EmployeeServiceRemote.java


import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public void doAction();  
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  public void removeEmployee(int id);
  public Employee raiseEmployeeSalary(int id, long raise) ;
  public Employee findEmployee(int id);
  public Collection<Employee> findAllEmployees() ;  
}

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.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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
    Employee emp = service.findEmployee(158);
    System.out.println(emp);
  }
}





Set Column Name For Entity Attribute

File: Employee.java
import java.io.Serializable;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="EMP")
public class Employee implements Serializable {
  private String firstName;
  private EmployeePK pk;
  public String getFirstName() { return firstName; }
  public void setFirstName(String firstName) { this.firstName = firstName; }
  @EmbeddedId
  @AttributeOverrides({
      @AttributeOverride(name="lastName", column=@Column(name="LAST_NAME")),
      @AttributeOverride(name="ssn", column=@Column(name="SSN"))
  })
  public EmployeePK getPk() { return pk; }
  public void setPk(EmployeePK pk) { this.pk = pk; }
  @Transient
  public String getLastName() { return pk.getLastName(); }
  @Transient
  public long getSsn() { return pk.getSsn(); }
}

File: EmployeePK.java
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class EmployeePK implements java.io.Serializable {
  private String lastName;
  private long ssn;
  public EmployeePK() {
  }
  public EmployeePK(String lastName, long ssn) {
    this.lastName = lastName;
    this.ssn = ssn;
  }
  @Column(name="CUSTOMER_LAST_NAME")
  public String getLastName() {
    return this.lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  @Column(name="CUSTOMER_SSN")
  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 EmployeePK))
      return false;
    EmployeePK pk = (EmployeePK) 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: EmployeeService.java

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public void doAction(){
    EmployeePK pk = new EmployeePK("B", 9999999);
    Employee cust = new Employee();
    cust.setPk(pk);
    cust.setFirstName("B");  
    
    em.persist(cust);
    em.flush();
    System.out.println("saved");
    
  }
}
    

File: EmployeeServiceLocal.java

import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
}

File: EmployeeServiceRemote.java


import java.util.Collection;
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 java.util.Collection;
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("EmployeeService/remote");
    
    
    
    
    service.doAction();
  }
}





Set Flush Mode

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  @PersistenceUnit(unitName="EmployeeService") private EntityManagerFactory factory;
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
    
    EntityManager createdManager = factory.createEntityManager();
    createdManager.setFlushMode(FlushModeType.ruMIT);
    
    Employee emp = new Employee(2);
    emp.setName("name");
    emp.setSalary(200);
        
    createdManager.persist(emp);
    //createdManager.flush();
    
    emp = em.find(Employee.class, 2);
    
    System.out.println(emp);
  }
}
    

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
}

File: EmployeeServiceRemote.java


import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  
  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.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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.doAction();
  }
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}





Use PersistenceContext annotation to Link Persistence Context

File: EmployeeService.java

import java.util.Collection;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    emp.setPicture(pic);
    
    em.persist(emp);
    
    
    emp = findEmployee(id);
    System.out.println(emp);
    
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
    Employee emp = new Employee(1);
    emp.setName("name");
    emp.setSalary(100);
    emp.setPicture("pic".getBytes());
    
    em.persist(emp);
    
    
    emp = findEmployee(1);
    System.out.println(emp);
  }
}
    

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
}

File: EmployeeServiceRemote.java


import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  
  public void doAction();
}

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  @Basic(fetch=LAZY)
  @Lob @Column(name="PIC")
  private byte[] picture;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  public byte[] getPicture() {
    return picture;
  }
  public void setPicture(byte[] picture) {
    this.picture = picture;
  }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

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.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("EmployeeService/remote");
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.doAction();
  }
}

File: persistence.xml
<persistence xmlns="http://java.sun.ru/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.ru/xml/ns/persistence http://java.sun.ru/xml/ns/persistence/persistence" version="1.0">
  <persistence-unit name="EmployeeService" > <!-- transaction-type="JTA" -->
        <jta-data-source>java:/DefaultDS</jta-data-source>
        <properties>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
         <property name="hibernate.show_sql" value="true"/>
        </properties>
<!--
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
      <property name="hibernate.connection.username" value="sa"/>
      <property name="hibernate.connection.password" value=""/>
      <property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/>
    </properties>
-->    
  </persistence-unit>
</persistence>





Use PersistenceUnit

File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
  @Id
  @Column(name = "EMP_ID")
  private int id;
  @Column(name = "COMM")
  private String name;
  @Column(name = "SAL")
  private long salary;
  
  public Employee() {
  }
  public Employee(int id) {
    this.id = id;
  }
  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 long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }

  public String toString() {
    return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
  }
}

File: EmployeeService.java

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  
  @PersistenceUnit(unitName="EmployeeService") private EntityManagerFactory factory;
  public EmployeeService() {
  }
  public Employee createEmployee(int id, String name, long salary, byte[] pic) {
    Employee emp = new Employee(id);
    emp.setName(name);
    emp.setSalary(salary);
    
    em.persist(emp);
    
    emp = findEmployee(id);
    System.out.println(emp);
    
    return emp;
  }
  public void removeEmployee(int id) {
    Employee emp = findEmployee(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
  public Employee raiseEmployeeSalary(int id, long raise) {
    Employee emp = em.find(Employee.class, id);
    if (emp != null) {
      emp.setSalary(emp.getSalary() + raise);
    }
    return emp;
  }
  public Employee findEmployee(int id) {
    return em.find(Employee.class, id);
  }
  public Collection<Employee> findAllEmployees() {
    Query query = em.createQuery("SELECT e FROM Employee e");
    return (Collection<Employee>) query.getResultList();
  }
  
  public void doAction(){
    EntityManager createdManager = factory.createEntityManager();
    
    
    Employee emp = new Employee(2);
    emp.setName("name");
    emp.setSalary(200);
        
    createdManager.persist(emp);
    createdManager.flush();
    
    emp = em.find(Employee.class, 2);
    
    System.out.println(emp);
  }
}
    

File: EmployeeServiceLocal.java

import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
    public void doAction();
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
}

File: EmployeeServiceRemote.java


import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
  public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
  
  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.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("EmployeeService/remote");
    
    
    
    service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
    service.doAction();
  }
}