Java/EJB3/Entity SecondaryTable

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

EJB Tutorial from JBoss: secondary table

   <source lang="java">

File: Customer.java /*

* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.tutorial.secondary.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.SecondaryTable; import javax.persistence.Table; @Entity @Table(name = "CUSTOMER") @SecondaryTable(name = "EMBEDDED_ADDRESS") // FIXME @JoinColumn(name = "ADDRESS_ID") public class Customer implements java.io.Serializable {

  private int id;
  private String first;
  private String last;
  private String street;
  private String city;
  private String state;
  private String zip;
  public Customer()
  {
  }
  public Customer(String first, String last, String street, String city, String state, String zip)
  {
     this.first = first;
     this.last = last;
     this.street = street;
     this.city = city;
     this.state = state;
     this.zip = zip;
  }
  @Id @GeneratedValue(strategy=GenerationType.AUTO)
  public int getId()
  {
     return id;
  }
  public void setId(int id)
  {
     this.id = id;
  }
  @Column(name = "FIRST")
  public String getFirst()
  {
     return first;
  }
  public void setFirst(String first)
  {
     this.first = first;
  }
  @Column(name = "LAST")
  public String getLast()
  {
     return last;
  }
  public void setLast(String last)
  {
     this.last = last;
  }
  @Column(name = "STREET", table = "EMBEDDED_ADDRESS")
  public String getStreet()
  {
     return street;
  }
  public void setStreet(String street)
  {
     this.street = street;
  }
  @Column(name = "CITY", table = "EMBEDDED_ADDRESS")
  public String getCity()
  {
     return city;
  }
  public void setCity(String city)
  {
     this.city = city;
  }
  @Column(name = "STATE", table = "EMBEDDED_ADDRESS")
  public String getState()
  {
     return state;
  }
  public void setState(String state)
  {
     this.state = state;
  }
  @Column(name = "ZIP", table = "EMBEDDED_ADDRESS")
  public String getZip()
  {
     return zip;
  }
  public void setZip(String zip)
  {
     this.zip = zip;
  }

}

File: CustomerDAO.java /*

* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.tutorial.secondary.bean; import java.util.List; public interface CustomerDAO {

  int create(String first, String last, String street, String city, String state, String zip);
  Customer find(int id);
  List findByLastName(String name);
  void merge(Customer c);

}

File: CustomerDAOBean.java /*

* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.tutorial.secondary.bean; import java.util.List; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext; @Stateless @Remote(CustomerDAO.class) public class CustomerDAOBean implements CustomerDAO {

  @PersistenceContext
  private EntityManager manager;
  public int create(String first, String last, String street, String city, String state, String zip)
  {
     Customer customer = new Customer(first, last, street, city, state, zip);
     manager.persist(customer);
     return customer.getId();
  }
  public Customer find(int id)
  {
     return manager.find(Customer.class, id);
  }
  public List findByLastName(String name)
  {
     return manager.createQuery("select c from Customer c where c.last = :name").setParameter("name", name).getResultList();
  }
  public void merge(Customer c)
  {
     manager.merge(c);
  }

}

File: Client.java /*

* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.tutorial.secondary.client; import org.jboss.tutorial.secondary.bean.Customer; import org.jboss.tutorial.secondary.bean.CustomerDAO; import javax.naming.InitialContext; import java.util.List;

public class Client {

  public static void main(String[] args) throws Exception
  {
     InitialContext ctx = new InitialContext();
     CustomerDAO dao = (CustomerDAO) ctx.lookup("CustomerDAOBean/remote");
     System.out.println("Create Bill Burke and Monica Smith");
     dao.create("Bill", "Burke", "1 Boston Road", "Boston", "MA", "02115");
     int moId = dao.create("Monica", "Smith", "1 Boston Road", "Boston", "MA", "02115");
     System.out.println("Bill and Monica get married");
     Customer monica = dao.find(moId);
     monica.setLast("Burke");
     dao.merge(monica);
     System.out.println("Get all the Burkes");
     List burkes = dao.findByLastName("Burke");
     System.out.println("There are now " + burkes.size() + " Burkes");
  }

}


      </source>
   
  
 
  



Entity With Secondary Table

   <source lang="java">

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");
   cust.setStreet("Street");
   cust.setCity("Boston");
   cust.setState("MA");
   
   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: Employee.java import java.io.Serializable;

import javax.persistence.*; @Entity @Table(name="Employee_TABLE") @SecondaryTable(name="ADDRESS_TABLE",

               pkJoinColumns={
                  @PrimaryKeyJoinColumn(name="ADDRESS_ID")})

public class Employee implements java.io.Serializable {

  private long id;
  private String firstName;
  private String lastName;
  private String street;
  private String city;
  private String state;
  @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; }
  @Column(name="STREET", table="ADDRESS_TABLE")
  public String getStreet() { return street; }
  public void setStreet(String street) { this.street = street; }
  @Column(name="CITY", table="ADDRESS_TABLE")
  public String getCity() { return city; }
  public void setCity(String city) { this.city = city; }
  @Column(name="STATE", table="ADDRESS_TABLE")
  public String getState() { return state; }
  public void setState(String state) { this.state = state; }

}

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


      </source>
   
  
 
  



Set Primary Key Join Column For Secondary Table

   <source lang="java">

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");
   cust.setStreet("Street");
   cust.setCity("Boston");
   cust.setState("MA");
   
   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: Employee.java import java.io.Serializable;

import javax.persistence.*; @Entity @Table(name="Employee_TABLE") @SecondaryTable(name="ADDRESS_TABLE",

               pkJoinColumns={
                  @PrimaryKeyJoinColumn(name="ADDRESS_ID")})

public class Employee implements java.io.Serializable {

  private long id;
  private String firstName;
  private String lastName;
  private String street;
  private String city;
  private String state;
  @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; }
  @Column(name="STREET", table="ADDRESS_TABLE")
  public String getStreet() { return street; }
  public void setStreet(String street) { this.street = street; }
  @Column(name="CITY", table="ADDRESS_TABLE")
  public String getCity() { return city; }
  public void setCity(String city) { this.city = city; }
  @Column(name="STATE", table="ADDRESS_TABLE")
  public String getState() { return state; }
  public void setState(String state) { this.state = state; }

}

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


      </source>