EJB3嵌入式:J2SE工程调用

自:http://hi.baidu.com/jt618/blog/item/e495164f69a47c33aec3ab2f.html

 

在JOBoss官网下载嵌入式开发包:

jboss-EJB-3.0_Embeddable_ALPHA_9

将jboss-EJB-3.0_Embeddable_ALPHA_9\bin\开发包导入到J2SE工程的Bin目录下

将jboss-EJB-3.0_Embeddable_ALPHA_9\ conf\开发包导入到J2SE工程的src目录下

将src目录引入数据源引用:META-INF\ persistence.xml

<persistence>

    <persistence-unit name="titan">

       <jta-data-source>java:/DefaultDS</jta-data-source>

       <properties>

       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

       </properties>

    </persistence-unit>

</persistence>

EntityBean(name=Cabin)

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name="TEST_CABIN")

public class Cabin implements java.io.Serializable{

    private Integer id;

    private String name;

    private Integer deskLevel;

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    public Integer getId() {

       return id;

    }

    public void setId(Integer id) {

       this.id = id;

    }  

    @Column(name="SYS_NAME")

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }  

    @Column(name="SYS_DESKLEVEL")

    public Integer getDeskLevel() {

       return deskLevel;

    }

    public void setDeskLevel(Integer deskLevel) {

       this.deskLevel = deskLevel;

    }  

}

//本地调用接口;

import javax.ejb.Local;

@Local

public interface CabinDAOLocal {

    public int create (Cabin cabin);

    public Cabin findById(int key);

}

//远程调用接口;

import javax.ejb.Remote;

@Remote

public interface CabinDAORemote {

    public int create (Cabin cabin);

    public Cabin findById(int key);

}

//接口实现类:

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

public @Stateless class CabinDAOBean implements CabinDAORemote,CabinDAOLocal {

    @PersistenceContext

    private EntityManager em;

    public int create(Cabin cabin) {

       em.persist(cabin);

       return cabin.getId();

    }

    public Cabin findById(int key) {

        Cabin cabin=em.find(Cabin.class,key);     

       return cabin;

    }

}

//本地调用测试

import javax.naming.InitialContext;

import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;

import com.j2se.Cabin;

import com.j2se.CabinDAOLocal;

public class TestJ2seLocalMain {  

    public static void main(String [] args) throws Exception

    {

       EJB3StandaloneBootstrap.boot(null);

       EJB3StandaloneBootstrap.scanClasspath();//扫描环境变量

       InitialContext ict=new InitialContext();

       CabinDAOLocal cabindaoremote=(CabinDAOLocal)ict.lookup("CabinDAOBean/local");

       Cabin cabin=new Cabin();

       cabin.setDeskLevel(1);

       cabin.setName("hubei");    

       int key=cabindaoremote.create(cabin);

       Cabin cabin1=cabindaoremote.findById(key);

       System.out.println(cabin1.getName());

       System.out.println(cabin1.getId());

       System.out.println(cabin1.getDeskLevel());

    }

}

 

//远程调用测式

import javax.naming.InitialContext;

import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;

import com.j2se.Cabin;

import com.j2se.CabinDAORemote;

public class TestJ2seRemoteMain {

    public static void main(String [] args) throws Exception

    {EJB3StandaloneBootstrap.boot(null);

       EJB3StandaloneBootstrap.scanClasspath();

       InitialContext ict=new InitialContext();

       CabinDAORemote cabindaoremote=(CabinDAORemote)ict.lookup("CabinDAOBean/remote");

       Cabin cabin=new Cabin();

       cabin.setDeskLevel(1);

       cabin.setName("hubei");    

       int key=cabindaoremote.create(cabin);

       Cabin cabin1=cabindaoremote.findById(key);

       System.out.println(cabin1.getName());

       System.out.println(cabin1.getId());

       System.out.println(cabin1.getDeskLevel());

    }

}

你可能感兴趣的:(Hibernate,jboss,J2SE,ejb,嵌入式)