//client 
public class EJBClient {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
try {
InitialContext ctx = new InitialContext(props);
BookDao bd = (BookDao) ctx.lookup("BookDaoImpl/remote");
System.out.println(bd);
Book book = new Book();
book.setName("book1234");
book.setAuthor("sasa");
book.setPrice(100);
bd.save(book);
} catch(NamingException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}//persistence.xml<?xml version="1.0"?>
<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="jpa" transaction-type="JTA">
     <jta-data-source>java:/MySqlDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.show_sql" value="true"/>
      </properties>
  </persistence-unit>
</persistence>//BookDaoImpl
@Stateless
@Remote(BookDao.class)
public class BookDaoImpl implements BookDao{
@PersistenceUnit(unitName="jpa")
private EntityManager em;

public BookDaoImpl() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa");
em = factory.createEntityManager();
}

public void save(Book book) {
System.out.println("into save-------------------");
em.persist(book);
} public void delete(int id) {
Book book = em.getReference(Book.class, id);
em.remove(book);
} public void update(int id, Book o) {
Book book = em.getReference(Book.class, id);
book.setName(o.getName());
book.setPrice(o.getPrice());
book.setAuthor(o.getAuthor());
}

public Book get(int id) {
return em.find(Book.class, id);
} public List<Book> findAll() {
String sql = "from Book";
List<Book> list = em.createQuery(sql).getResultList();
return list;
}
}@Entity @Table(name="book")
public class Book implements Serializable{
private int id;
private String name;
private String author;
private double price;

@Id @GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Column(length=50)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Column
public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} @Column(length=50)
public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
}
主要代码就这些
我把 我 的  项目的  Dao 和 实体bean都打好包了放在 ejb 也发布成功啦 。。//不然也不会运行下面第一句打印。
还有 BookDaoImpl 里面的 方法也没问题 但 一运行就出错 大家帮我 看看。。 但是我 一运行 就出下面错误
jboss.j2ee:jar=testEJB.jar,name=BookDaoImpl,service=EJB3
Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
......
Caused by: java.lang.reflect.InvocationTargetException'
......
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
.....
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
......

解决方案 »

  1.   

    http://topic.csdn.net/u/20070809/11/0d9a306d-8dec-4013-8ab7-e79d6dd73da6.html
      

  2.   

    ejb还没用过,只是听说这个挺强大的,先顶了
      

  3.   

    public BookDaoImpl() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa");
    em = factory.createEntityManager();
    } 构造中的那两行语句可以删掉
    使用 Persistence.createEntityManagerFactory 创建 EntityManagerFactory 对象的方式是 JPA 在 J2SE 环境中使用的,如果在 EJB 容器或者 J2EE 应用服务器中使用的话,是不能在 EJB 中这样创建的。在 J2EE 环境中,我们一般只需要注入 EntityManager 对象就可以了,这比 J2SE 环境下使用 JPA 方便数倍,只要加下面这样的一行代码,EJB 容器就会自动创建 EntityManagerFactory 对象,并且生成一个 EntityManager 对象注入到 EJB 中去。@PersistenceContext(unitName="jpa")
    private EntityManager em; 如果只有一个数据源的话,只标注 @PersistenceContext 就可以了。
    除了构造中的两行代码需要删掉之外,下面的也需要注意一下:@PersistenceUnit(unitName="jpa")
    private EntityManager em; 改为:@PersistenceContext(unitName="jpa")
    private EntityManager em; @PersistenceUnit 是用于注入 EntityManagerFactory 对象的,不是用于注入 EntityManager 对象的。
      

  4.   

    Book book = em.getReference(Book.class, id);使用 getReference 方法需要能通过主键查找到数据对象,但是如果数据表中不存在这个 id 的数据,使用这个方法时就会抛出异常,这在事务处理中是很有用处的,其会自动回滚事务。如果需要在找不到该条数据时做些处理,那么应该使用 find 方法,同样是通过主键去找数据对象,唯一不同的是 find 如果找不到的话不会抛出异常,而是返回 null。PS:楼主或许没注意到,在 EJB 中没有事务处理语句,但是更新也进行了事务提交。这主要是得益于 EJB 的方法都存在事务,其默认的事务传播属性为 @TransactionAttribute(TransactionAttributeType.REQUIRED)