ITeye博客

hibernate

sdkongkong · 2011-11-01 05:39 · persistence.xml · 开源软件 · Markdown

1 添加hibernate3.jar 和其他所有jar, 数据库驱动jar包 2 在src目录下面 META-INF 下面建立 persistence.xml,内容为 org.hibernate.ejb.HibernatePersistence 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(); } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。