Java/EJB3/Annotation

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

Discriminator Value in entity hierarchy

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(){
    Person p = new Person();
    p.setFirstName("P");
    p.setLastName("P");
    em.persist(p);
    Customer cust = new Customer();
    cust.setFirstName("C");
    cust.setLastName("C");
    cust.setStreet("Street");
    cust.setCity("City");
    cust.setState("State");
    cust.setZip("11111");
    em.persist(cust);
    Employee employee = new Employee();
    employee.setFirstName("E");
    employee.setLastName("E");
    employee.setStreet("1st Street");
    employee.setCity("E City");
    employee.setState("E State");
    employee.setZip("33333");
    employee.setEmployeeId(15);
    em.persist(employee);
    
    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: Employee.java
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
public class Employee extends Customer {
   private int employeeId;
   public int getEmployeeId() { return employeeId; }
   public void setEmployeeId(int id) { employeeId = id; }
}

@Entity
@DiscriminatorValue("CUST")
class Customer extends Person {
   private String street;
   private String city;
   private String state;
   private String zip;
   
   public String getStreet() { return street; }
   public void setStreet(String street) { this.street = street; }
   public String getCity() { return city; }
   public void setCity(String city) { this.city = city; }
   public String getState() { return state; }
   public void setState(String state) { this.state = state; }
   public String getZip() { return zip; }
   public void setZip(String zip) { this.zip = zip; }
}

@Entity
@Table(name="PERSON_HIERARCHY")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIMINATOR",
                     discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("PERSON")
class Person implements java.io.Serializable
{
   private int id;
   private String firstName;
   private String lastName;
   @Id @GeneratedValue
   public int getId() { return id; }
   public void setId(int 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; }
}
    

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





Mark Ejb Method With Our Own Annotation

File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int 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;
  }
}

File: EmployeeService.java
import javax.ejb.Stateless;
import javax.transaction.TransactionManager;
@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  public EmployeeService() {
  }
  
  @JndiInjected("java:/TransactionManager")
  TransactionManager tm;
  public void doAction() throws Exception {
    System.out.println("Is there a transaction: " + (tm.getTransaction() != null));
  }
}

File: EmployeeServiceLocal.java

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

}

File: EmployeeServiceRemote.java


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

File: JndiInjected.java

import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JndiInjected {
   String value();
}

File: JndiInjector.java

import java.lang.reflect.*;
import javax.ejb.*;
import javax.interceptor.*;
import javax.naming.*;
import javax.annotation.PostConstruct;
public class JndiInjector {
   @PostConstruct
   public void jndiInject(InvocationContext invocation) {
      Object target = invocation.getTarget();
      Field[] fields = target.getClass().getDeclaredFields();
      Method[] methods = target.getClass().getDeclaredMethods();
      // find all @JndiInjected fields methods and set them
      try {
         InitialContext ctx = new InitialContext();
         for (Method method : methods) {
            JndiInjected inject = method.getAnnotation(JndiInjected.class);
            if (inject != null) {
               Object obj = ctx.lookup(inject.value());
               method.setAccessible(true);
               method.invoke(target, obj);
            }
         }
         for (Field field : fields) {
            JndiInjected inject = field.getAnnotation(JndiInjected.class);
            if (inject != null) {
               Object obj = ctx.lookup(inject.value());
               field.setAccessible(true);
               field.set(target, obj);
            }
         }
         invocation.proceed();
      } catch (Exception ex) {
         throw new EJBException("Failed to execute @JndiInjected", ex);
      }
   }
}

File: Main.java
import java.util.Date;
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