`

JPA(HIberante)环境的搭建

阅读更多

1.添加jar包

hibernate & jpa jar(15):
	hibernate-distribution-3.3.1.GA/hibernate3.jar
	hibernate-distribution-3.3.1.GA/lib/required/antlr-2.7.6.jar
	hibernate-distribution-3.3.1.GA/lib/required/dom4j-1.6.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/commons-collections-3.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/jta-1.1.jar
	hibernate-distribution-3.3.1.GA/lib/required/javassist-3.4.GA.jar
	hibernate-distribution-3.3.1.GA/lib/required/slf4j-api-1.5.2.jar
	hibernate-distribution-3.3.1.GA/lib/bytecode/cglib/hibernate-cglib-repack-2.1_3.jar
	hibernate-distribution-3.3.1.GA/lib/optional/ehcache-1.2.jar
	
	hibernate-annotations-3.4.0.GA/hibernate-annotations.jar
	hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
	hibernate-annotations-3.4.0.GA/lib/test/slf4j-log4j12.jar
	hibernate-annotations-3.4.0.GA/lib/test/log4j.jar
	hibernate-annotations-3.4.0.GA/lib/ejb3-persistence.jar

	hibernate-entitymanager-3.4.0.GA/lib/hibernate-entitymanager.jar

 2.在src创建META-INF文件夹,在其下面创建persistence.xml文件,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<persistence-unit name="ssj_unit" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<!--
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
			<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
			<property name="hibernate.connection.username" value="yxuser" />
			<property name="hibernate.connection.password" value="yxuser" />
			<property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.1.96:1521:yxdb" />
			-->
			<!-- 连接参数 -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="root" />
			<property name="hibernate.connection.url"
				value="jdbc:mysql://localhost:3306/ssj?useUnicode=true&amp;characterEncoding=UTF-8" />
			
			<!-- 数据源设置 -->
			<property name="hibernate.max_fetch_depth" value="3" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<property name="hibernate.jdbc.fetch_size" value="50" />
			<property name="hibernate.jdbc.batch_size" value="10" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
		</properties>
	</persistence-unit>

</persistence>

 3.创建实体类,其内容如下:

package com.taoistwar.ssj.producty.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ProductType {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
	private String name;

	public ProductType() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 4.创建测试类

package test.producty.entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

import com.taoistwar.ssj.producty.entity.ProductType;

public class ProductTypeTest {

	@Test
	public void testRun() {
		try {
			EntityManagerFactory emf =  Persistence
					.createEntityManagerFactory("ssj_unit");
			EntityManager em = emf.createEntityManager();
			em.getTransaction().begin();
			ProductType pt = new ProductType();
			pt.setName("中文");
			em.persist(pt);
			em.getTransaction().commit();
			em.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics