脱离容器的EJB
来源:互联网 发布:c语言乘法游戏编程 编辑:程序博客网 时间:2024/06/02 11:40
需要引入的包 hibernate-annotations,hibernate,hibernate-entitymanager 这3个包及附带的包
配置文件META-INF/persistence.xml
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.foshanshop.ejb3.bean.Person</class>
<class>com.foshanshop.ejb3.bean.Order</class>
<class>com.foshanshop.ejb3.bean.OrderItem</class>
<class>com.foshanshop.ejb3.bean.Teacher</class>
<class>com.foshanshop.ejb3.bean.Student</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="hibernate.connection.url"
value="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=EJB" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.connection.username"
value="ls" />
<property name="hibernate.connection.password"
value="123456" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.foshanshop.ejb3.bean.Person</class>
<class>com.foshanshop.ejb3.bean.Order</class>
<class>com.foshanshop.ejb3.bean.OrderItem</class>
<class>com.foshanshop.ejb3.bean.Teacher</class>
<class>com.foshanshop.ejb3.bean.Student</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="hibernate.connection.url"
value="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=EJB" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.connection.username"
value="ls" />
<property name="hibernate.connection.password"
value="123456" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
持久类
package com.foshanshop.ejb3.bean;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.GenerationType;
@Entity
public class Person implements Serializable ...{
@Id
@GeneratedValue
private Integer personid;
@Column(nullable = false, length = 32)
private String name;
private boolean sex;
private Short age;
private Date birthday;
public Integer getPersonid() ...{
return personid;
}
public void setPersonid(Integer personid) ...{
this.personid = personid;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
// @Column(nullable=false)
public boolean getSex() ...{
return sex;
}
public void setSex(boolean sex) ...{
this.sex = sex;
}
@Column(nullable = false)
public Short getAge() ...{
return age;
}
public void setAge(Short age) ...{
this.age = age;
}
public Date getBirthday() ...{
return birthday;
}
public void setBirthday(Date birthday) ...{
this.birthday = birthday;
}
}
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.GenerationType;
@Entity
public class Person implements Serializable ...{
@Id
@GeneratedValue
private Integer personid;
@Column(nullable = false, length = 32)
private String name;
private boolean sex;
private Short age;
private Date birthday;
public Integer getPersonid() ...{
return personid;
}
public void setPersonid(Integer personid) ...{
this.personid = personid;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
// @Column(nullable=false)
public boolean getSex() ...{
return sex;
}
public void setSex(boolean sex) ...{
this.sex = sex;
}
@Column(nullable = false)
public Short getAge() ...{
return age;
}
public void setAge(Short age) ...{
this.age = age;
}
public Date getBirthday() ...{
return birthday;
}
public void setBirthday(Date birthday) ...{
this.birthday = birthday;
}
}
测试类:
package com.foshanshop.ejb3;
import java.text.SimpleDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import com.foshanshop.ejb3.bean.Person;
public class Example ...{
EntityManagerFactory emf;
public Example() ...{
emf = Persistence.createEntityManagerFactory("example");//example 为配置文件中指定
}
public void exercise() throws Exception ...{
Person p = new Person();
p.setName("John Doef1df2");
p.setAge((short) 25);
p.setSex(false);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
p.setBirthday(sf.parse("1990-02-01"));
p.setD("abc");
savePerson(p);
Person py = findByName("John Doef1df2");
System.out.println(py);
emf.close();
}
private Person findByName(String name) ...{
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select person from Person as person where name=:param");
q.setParameter("param", name);
Person p = (Person) q.getSingleResult();
em.close();
return p;
}
private Person findById(Long id) ...{
EntityManager em = emf.createEntityManager();
Person p = em.find(Person.class, id);
em.close();
return p;
}
private int savePerson(final Person p) ...{
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Person pp = em.merge(p);
tx.commit();
em.close();
return pp.getPersonid();
}
public static void main(String[] args) throws Exception ...{
Example example = new Example();
example.exercise();
try ...{
Thread.sleep(30000);
}
catch (InterruptedException ex) ...{
}
}
}
import java.text.SimpleDateFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import com.foshanshop.ejb3.bean.Person;
public class Example ...{
EntityManagerFactory emf;
public Example() ...{
emf = Persistence.createEntityManagerFactory("example");//example 为配置文件中指定
}
public void exercise() throws Exception ...{
Person p = new Person();
p.setName("John Doef1df2");
p.setAge((short) 25);
p.setSex(false);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
p.setBirthday(sf.parse("1990-02-01"));
p.setD("abc");
savePerson(p);
Person py = findByName("John Doef1df2");
System.out.println(py);
emf.close();
}
private Person findByName(String name) ...{
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select person from Person as person where name=:param");
q.setParameter("param", name);
Person p = (Person) q.getSingleResult();
em.close();
return p;
}
private Person findById(Long id) ...{
EntityManager em = emf.createEntityManager();
Person p = em.find(Person.class, id);
em.close();
return p;
}
private int savePerson(final Person p) ...{
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Person pp = em.merge(p);
tx.commit();
em.close();
return pp.getPersonid();
}
public static void main(String[] args) throws Exception ...{
Example example = new Example();
example.exercise();
try ...{
Thread.sleep(30000);
}
catch (InterruptedException ex) ...{
}
}
}
- 脱离容器的EJB
- EJB 容器
- EJB----容器
- 开发EJB容器模型的WEB服务(EJB WebService)
- Jboss的EJB容器的相关文档
- 从EJB容器得到的分布式灵感
- Java EJB容器的存取和实现
- Java使用EJB容器的详细概述
- 开发EJB容器模型的WEB服务
- jndi(是什么)和ejb容器的关系
- EJB体系结构之EJB容器
- JavaEE 的EJB容器搭建的错误与解决方法
- 脱离容器 使用 jpa persistence.xml 模板 上面用的数据库是Derby
- spring 之脱离容器管理创建的对象进行依赖注入
- 重写ejb的helloworld程序,部署容器为JBoss
- 作为容器应该具有的特点(without ejb)
- EJB中的容器托管的EntityManager持久化上下文
- 对于jboss EJB容器管理事务的问题
- DOS命令集合(某些同志要好好学习学习才行了)
- 什么是blog?
- Tips for Appfuse2.01
- 常用类 java.util.Date
- WinCE开发资源
- 脱离容器的EJB
- 类 java.io.File
- Debian下安装中文输入法
- 使用Eclipse进行SWT编程(1)
- Ajax原则[摘录]
- PHP5跟apache2.0安装不能解析PHP代码的解决办法
- 使用Eclipse进行SWT编程(2)
- 新年了!压力越来越大!
- SIP协议解析与实现(c和c++ 使用osip) 9