1 添加hibernate3.jar 和其他所有jar, 数据库驱动jar包
2 在src目录下面 META-INF 下面建立 persistence.xml,内容为
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="babasports" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/babasports" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
3 编写producttype.java
package com.sdkongkong.bean;
import javax.persistence.*;
@Entity
public class ProductType {
private Integer typeId;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Integer getTypeId() {
System.out.println("get type id");
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
}
4 编写测试类 producttest.java
package junit.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
import com.sdkongkong.bean.ProductType;
public class ProductTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void runtest(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("babasports");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new ProductType());
em.getTransaction().commit();
em.close();
factory.close();
}
}
2 在src目录下面 META-INF 下面建立 persistence.xml,内容为
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="babasports" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/babasports" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
3 编写producttype.java
package com.sdkongkong.bean;
import javax.persistence.*;
@Entity
public class ProductType {
private Integer typeId;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Integer getTypeId() {
System.out.println("get type id");
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
}
4 编写测试类 producttest.java
package junit.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
import com.sdkongkong.bean.ProductType;
public class ProductTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void runtest(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("babasports");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new ProductType());
em.getTransaction().commit();
em.close();
factory.close();
}
}