1:导入jpa的jar包
不同jpa实现导入的jar包是不同的,如果是用hibernate来实现的,那么就要导入hibernate实现的jia包。这里因为是用Hibernate来实现的,
所以就需要两个,一个是Hibernate的核心包(hibernate-3.2文件里相应的jar包,就是hibernate的核心包hibernate3.jar和lib文件夹下所有
的第三方实现.jar文件);还有一个就是hibernate对jpa规范实现的包(hibernate-entitymanager-3.3.2.CR1文件中的hibernate-
entitymanager.jar文件,和lib文件夹下的三个.jar文件间,即ejb3-persistence.jar,hibernate-annotations.jar,hibernate-common-
annotations.jar).
2:jpa配置文件的编写:
在项目src目录下新建一个META-INF文件夹(而不是在WEB-INF目录下的META-INF目录下建立persistence.xml),在该文件夹下面建立一个
persistence.xml文件,至此jpa开发环境就搭建好了。
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">
<!-- file:///D:/hibernate/hibernate-entitymanager-3.3.2.CR1/resources/org/hibernate/ejb/persistence_1_0.xsd -->
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?
useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<!--value="update",update属性1:当数据库中对应实体bean的表不存在的时候,创建表
2:当表存在的时候,如果表的结构发生变化,比如说增加字段的话,就会自动更新表的结构-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
3:jpa环境测试
类Persistence是jpa框架的一个入口点,
测试方法:
public void test(){
//其中"itcast"是在persistense.xml中<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">指定的。
Persistence.createEntityManagerFactory("itcast");
}
假如说这是有一个实体bean,名称为ProductType.java,那么运行上述test()方法的话,就会在数据库里生成出一张表(数据库也是要体提前建好的),表的名字是producttype,
ProductType.java:
package com.itcast.bean.product;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class ProductType implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer typeid;
private String name;
//用于google描述
private String note;
private Boolean visible = true;
/**
* 子类别
*/
private Set<ProductType> childtypes= new HashSet<ProductType>();
/**
* 父类别
* @return
*/
private ProductType parent;
public ProductType(){
}
public ProductType(int typeid){
this.typeid = typeid;
}
public ProductType(String name,String note){
this.name = name;
this.note = note;
}
@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")//mappedBy是说父类是被管的
public Set<ProductType> getChildtypes() {
return childtypes;
}
public void setChildtypes(Set<ProductType> childtypes) {
this.childtypes = childtypes;
}
@ManyToOne(cascade={CascadeType.REFRESH},optional=true)//optional=false说明父类是可选的,不是每个子类都必须有父类的
@JoinColumn(name="parentid")//在子类生成的表中添加一个外键字段parentid,指向父类的表(也可以是同一张表)
public ProductType getParent() {
return parent;
}
public void setParent(ProductType parent) {
this.parent = parent;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
@Column(length=36,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=200)
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Column(nullable=false)
public Boolean getVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((typeid == null) ? 0 : typeid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ProductType other = (ProductType) obj;
if (typeid == null) {
if (other.typeid != null)
return false;
} else if (!typeid.equals(other.typeid))
return false;
return true;
}
}
总结:jpa框架是与web无关的,和tomcat服务器也是无关的,并不是tomcat服务器启动的时候自动创建数据表的。