EJB3.0容器内外调用总结

1、Entity与表关联(注解)以用户和联系人为例(一对多关系)
 A、联系人实体
  @Entity
  @Table(name = "TLInkMan")
  public class LinkMan implements Serializable {
   private static final long serialVersionUID = -2234837746464950325L;
   //联系人ID表中设定为char(32)类型,需导入hibernate相关包
   @Id
   @Column(name = "linkman_id")
   @GenericGenerator(name = "linkManIdGenerator", strategy = "uuid")
   @GeneratedValue(generator = "linkManIdGenerator")
   private String linkManId;
   @Column(name = "linkman_name")
   private String linkManName;
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User user;
   ......
  }
 B、用户实体
  @Entity
  @Table(name = "TUser")
  public class User implements Serializable {
   @Id
   @Column(name="user_id")
   @GenericGenerator(name = "userIdGenerator", strategy = "uuid")
   @GeneratedValue(generator="userIdGenerator")
   private String userId;
   @OneToMany(cascade=CascadeType.REMOVE)//容许级联删除
   private Set<LinkMan> linkMen;
   ......
  }
2、接口和实现类头注解
 A、接口
  @Remote//远程或者Local本地;
  public interface LinkManService
 B、实现类
  @Stateless//无状态或者Stateful有状态;
  public class LinkManBean implements LinkManService
3、在persistent.xml文件中描述全部数据库信息,容器内调用
 A、persistent.xml文件中描述:
  <?xml version="1.0" encoding="UTF-8" ?>
  <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" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="持久化单元名称">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.connection.driver_class" value="数据库驱动" />
    <property name="hibernate.connection.url" value="数据库连接字符串" />
    <property name="hibernate.connection.username" value="用户名" />
    <property name="hibernate.connection.password" value="密码" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
   </properties>
  </persistence-unit>
  </persistence>
 B、serviceImpl业务接口实现类中:
  public  void operat() {
   //创建一个实体管理工厂
   EntityManagerFactory emf = Persistence
     .createEntityManagerFactory("持久化单元名称");
   //创建一个实体管理对象
   EntityManager em = emf.createEntityManager();
   //创建一个事务
   EntityTransaction transaction = em.getTransaction();
   //开始事务
   transaction.begin();
   em.persist(实体);
   em.createQuery(查询) 
   //提交事务
   transaction.commit();

  }
4、在jboss中配置好mssql-ds.xml(以sqlserver2005为例)数据源,容器外调用
 A、mssql-ds.xml配置:
  <datasources>
    <local-tx-datasource>
      <jndi-name>数据源</jndi-name>
      <connection-url>jdbc:sqlserver://localhost:1433;DatabaseName=数据库名</connection-url>
      <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
      <user-name>x</user-name>
      <password>y</password>     
        <metadata>
    <type-mapping>MS SQLSERVER2005</type-mapping>
        </metadata>
    </local-tx-datasource>

  </datasources>
 B、persistent.xml文件中描述:
  <?xml version="1.0" encoding="UTF-8" ?>
  <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" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="持久化单元名称"> 
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <jta-data-source>java:数据源</jta-data-source>
   <properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
   </properties>
  </persistence-unit>
  </persistence>
 C、serviceImpl业务接口实现类中:
  @PersistenceContext
  private EntityManager em; //定义一个实体管理对象
  public  void operat() {
   em.persist(实体);
  }
 D、外部访问:
  Properties props = new Properties();
  props.setProperty("java.naming.factory.initial",
    "org.jnp.interfaces.NamingContextFactory");
  props.setProperty("java.naming.provider.url", "localhost:1099");
  props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
  InitialContext ctx;
  try {
   ctx = new InitialContext(props);
   //Interface ----业务接口
   Interface objectInterface = (Interface) ctx
     .lookup("接口实现类名/remote");//remote或local
   objectInterface.operat();

  } catch (NamingException e) {
   e.printStackTrace();
  }

你可能感兴趣的:(Hibernate,xml,jboss,配置管理,sun)