EJB JPA 事物管理,用JBOSS发布
persistence.xml为<?xml version="1.0" encoding="utf-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="demo"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/OracleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
实体BEAN为[/color
package com.test;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
public class CustomerEO implements Serializable{
private Integer id;
private String name;
private String shortname;
private Double registeredCapital;
@Id
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "registered_capital")
public Double getRegisteredCapital() {
return registeredCapital;
}
public void setRegisteredCapital(Double registeredCapital) {
this.registeredCapital = registeredCapital;
}
@Column(name = "short_name")
public String getShortname() {
return shortname;
}
public void setShortname(String shortname) {
this.shortname = shortname;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((id == null) ? 0 : id.hashCode());
result = PRIME * result + ((name == null) ? 0 : name.hashCode());
result = PRIME * result + ((registeredCapital == null) ? 0 : registeredCapital.hashCode());
result = PRIME * result + ((shortname == null) ? 0 : shortname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final CustomerEO other = (CustomerEO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (registeredCapital == null) {
if (other.registeredCapital != null)
return false;
} else if (!registeredCapital.equals(other.registeredCapital))
return false;
if (shortname == null) {
if (other.shortname != null)
return false;
} else if (!shortname.equals(other.shortname))
return false;
return true;
}
}
[color=#FF0000]服务接口
package com.test;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface ICustomerService {
public void save(CustomerEO transientInstance);
public void delete(Integer id);
public CustomerEO update(CustomerEO detachedInstance);
public CustomerEO findById(Integer id);
public List<CustomerEO> findAll();
}
以下为调用的类
package com.test;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.test.ICustomerService;
public @Stateless
class CustomerService implements ICustomerService {
@PersistenceContext(unitName = "demo")
private EntityManager entityManager;
public void delete(Integer id) {
// TODO Auto-generated method stub
CustomerEO instance = this.findById(id);
entityManager.remove(instance);
}
public List<CustomerEO> findAll() {
// TODO Auto-generated method stub
String sql = "SELECT C FROM CustomerEO c";
Query query = entityManager.createQuery(sql);
return query.getResultList();
}
public CustomerEO findById(Integer id) {
// TODO Auto-generated method stub
CustomerEO instance = entityManager.find(CustomerEO.class, id);
return instance;
}
public void save(CustomerEO transientInstance) {
// TODO Auto-generated method stub
entityManager.persist(transientInstance);
}
public CustomerEO update(CustomerEO detachedInstance) {
// TODO Auto-generated method stub
CustomerEO result = entityManager.merge(detachedInstance);
return result;
}
}
下面为测试类
package com.test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class Client {
private static Context context;
public static void main(String[] args) {
try {
Context ict = getInitialContext();
Object obj = ict.lookup("CustomerService/remote");
ICustomerService service=(ICustomerService)PortableRemoteObject.narrow(obj, ICustomerService.class);
CustomerEO ce=new CustomerEO();
ce.setId(14);
ce.setName("名字");
ce.setRegisteredCapital(10.2);
ce.setShortname("简介");
service.save(ce);
} catch (NamingException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static Context getInitialContext() throws Exception {
if (context == null) {
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
context = new InitialContext(props);
}
return context;
}
}