Java/EJB3/Entity Primary Key

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

EJB Tutorial from JBoss: Primary Key Composition

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.ruposite.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
/**
 *
 */
@Entity
public class Customer implements java.io.Serializable
{
   CustomerPK pk;
   Set<Flight> flights;
   public Customer()
   {
   }
   @EmbeddedId
   public CustomerPK getPk()
   {
      return pk;
   }
   public void setPk(CustomerPK pk)
   {
      this.pk = pk;
   }
   @Transient
   public String getName()
   {
      return pk.getName();
   }
   @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy="customers")
   @JoinTable(name="flight_customer_table",
              joinColumns = {@JoinColumn(name = "FLIGHT_ID")},
              inverseJoinColumns = {@JoinColumn(name = "CUSTOMER_ID"), @JoinColumn(name = "CUSTOMER_NAME")})
   public Set<Flight> getFlights()
   {
      return flights;
   }
   public void setFlights(Set<Flight> flights)
   {
      this.flights = flights;
   }
}

File: CustomerPK.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.ruposite.bean;
import javax.persistence.Embeddable;
import javax.persistence.Embeddable;
/**
 * Comment
 *
 * @author 



== Use Combined Fields As Primary Key ==






   
  <!-- start source code -->
   
    <source lang="java">

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 void doAction(){
    Employee cust = new Employee();
    cust.setFirstName("B");
    cust.setLastName("B");
    cust.setSsn(9999999);    
    
    em.persist(cust);
    em.flush();
    
  }

}
    

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.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="EMP")
@IdClass(EmployeePK.class)
public class Employee implements 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; }
  public String toString() {
    return "Employee : " + getFirstName() ;
  }
}

File: EmployeePK.java
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;
  }
  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 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: 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