标签:style blog io color ar os 使用 java sp
1、编写实体类,代码如下:
package learn.jpa.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Person { @Id @GeneratedValue private Integer id; private String name; public Person(){} public Person(String name){ this.name = name; } 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; } }
@Id用于标记属性的主键
@GeneratedValue
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
2、单元测试类,代码如下:
package learn.jpa.junit.test; import static org.junit.Assert.*; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import learn.jpa.bean.Person; import org.junit.Test; public class PersonTest { @Test public void save(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // 开启事务 em.persist(new Person("hwl")); em.getTransaction().commit(); em.close(); factory.close(); } }
(1)、表什么时候被创建?
在执行完一下语句的时候表被创建:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");
如果表没有被创建,则说明些的 Entity bean 有问题。
标签:style blog io color ar os 使用 java sp
原文地址:http://www.cnblogs.com/hwlsniper/p/4079714.html