1、常用格式实体Bean
package cn.myweb.entity;
import java.io.Serializable;
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 = "detail")//对应数据库的表名
public class Detail implements Serializable {
@Id
@Column(name = "id", nullable = false) //对应表中的id字段,且不能未空
@GeneratedValue(strategy = GenerationType.AUTO)//ID递增
private Integer id;
@Column(name = "name", nullable = false) //对应表中的name字段,且不能未空
private String name;
//普通的get,set,equals,toString方法
}
2、常用格式会话Bean
a)会话Bean远程接口(DetailRemote.java)
package cn.myweb.session;
import javax.ejb.Remote;
@Remote
public interface DetailRemote {
public Detail saveOrUpdateDetail(Detail detail);
public Detail getDetailById(int id);
public List queryDetail(String nameStr);
public boolean deleteDetail(Detail detail);
}
b)会话Bean本地接口(DetailLocal.java)
package cn.myweb.session;
import javax.ejb.Local;
@Local
public interface DetailLocal extends DetailRemote{}
c)会话Bean(DetailBean.java)
package cn.myweb.session;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.sql.DataSource;
import cn.myweb.entity.Detail;
@Stateless(name="detail")
public class DetailBean implements DetailLocal,DetailRemote{
@PersistenceContext(unitName="mis")
private EntityManager em;
@Resource (mappedName="java:/MisDS")//Jdbc可以的使用的数据源
DataSource ds;
public Detail saveOrUpdateDetail(Detail detail){ //新增或者更新
try{
detail=em.merge(detail);
}catch(Exception e){
//doing();处理
}
return detail;
}
public Detail getDetailById(int id){ //查询
Detail detail=null;
try{
detail = em.find(Detail.class,id);
}catch(Exception e){
//doing();处理
}
return detail;
}
public List queryDetail(String nameStr){//查询
List result=null;
try{
String sql="select d from Detail d where d.name like :name ";
Query query = em.createQuery(sql);
query.setParameter("name",nameStr+"%");
result = query.getResultList();
}catch(Exception e){
//doing();处理
}
return result;
}
public boolean deleteDetail(Detail detail){//删除
try{
em.remove(detail);
return true;
}catch(Exception e){
//doing();处理
return false;
}
}
}
3、配置文件(放在工程的src目录下)
a)JNDI属性配置(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
b)日志文件配置(log4j.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n"/>
</layout>
</appender>
<root>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
c) 数据源配置(persistence.xml)
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="mis"><!--ejb3中EntityManager用到 -->
<jta-data-source>java:/MisDS</jta-data-source><!--和数据源文件informix-ds.xml一致-->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
4、打包文件(packaging-build.xml),放在工程下面。选择工程,点右键选择'属性',然后在'packaging Configuration'面板中勾上'Enable packaging'。
<?xml version="1.0" encoding="UTF-8"?>
<project name="Packaging Generator" default="_packaging_generation_">
<target name="_packaging_generation_" depends="N65557"/>
<target name="N65557" description="MyModule.jar">
<jar destfile="MyModule.jar">
<zipfileset dir="bin" prefix="META-INF">
<include name="persistence.xml"/>
</zipfileset>
<!--将persistence.xml,bin/cn/myweb/entity/和bin/cn/myweb/session目录下所有的class文件打包为MyModule.jar包-->
<zipfileset dir="bin" includes="cn/myweb/entity/*.class,cn/myweb/session/*.class"/>
</jar>
</target>
</project>
5、配置DateSource(informix-ds.xml)
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MisDS</jndi-name>
<connection-url>jdbc:informix-sqli://192.168.168.168:6666/basename:informixserver=online_web</connection-url>
<driver-class>com.informix.jdbc.IfxDriver</driver-class>
<user-name>username</user-name>
<password>password</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
</local-tx-datasource>
</datasources>
posted on 2007-06-12 12:46
破茧而出 阅读(647)
评论(0) 编辑 收藏 所属分类:
Hibernate_EJB