Java/EJB3/Entity Merge
EJB Tutorial from JBoss: merge entity back
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.merge.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.merge.bean;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
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: 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.merge.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;
@Entity
@Table(name = "CUSTOMER")
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")
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;
}
@Column(name = "ZIP")
public String getZip()
{
return zip;
}
public void setZip(String zip)
{
this.zip = zip;
}
}
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.merge.client;
import org.jboss.tutorial.merge.bean.Customer;
import org.jboss.tutorial.merge.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");
}
}
Entity With Transient Fields
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();
}
}