| |
使用域的联合主键 |
|
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
|
|
EJB-UseCombinedFieldsAsPrimaryKey.zip( 4,489 k) |
Related examples in the same category |
|