标签:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="NewPersistenceUnit"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>Hello.Customer</class> <properties> <!-- 连接数据库的基本信息 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///test"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="root"/> <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 --> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- 二级缓存相关 --> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/> <property name="hibernate.cache.use_query_cache" value="true"/> </properties> </persistence-unit> </persistence>
package Hello; import javax.persistence.*; @NamedQuery(name="testNamedQuery", query="FROM Customer c WHERE c.id = ?") @Cacheable(true) @Table(name="JPA_CUTOMERS") @Entity public class Customer { private Integer id; private String lastName; private String email; private int age;
public Customer() { // TODO Auto-generated constructor stub } public Customer(String lastName, int age) { super(); this.lastName = lastName; this.age = age; } @GeneratedValue(strategy= GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="LAST_NAME",length=50,nullable=false) public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Hello.Customer [id=" + id + ", lastName=" + lastName + ", email=" + email + ", age=" + age + "]"; } }
package Hello; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { //1. 创建 EntitymanagerFactory String persistenceUnitName = "NewPersistenceUnit"; Map<String, Object> properites = new HashMap<String, Object>(); properites.put("hibernate.show_sql", true); EntityManagerFactory entityManagerFactory = //Persistence.createEntityManagerFactory(persistenceUnitName); Persistence.createEntityManagerFactory(persistenceUnitName, properites); //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory EntityManager entityManager = entityManagerFactory.createEntityManager(); //3. 开启事务 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); //4. 进行持久化操作 Customer customer = new Customer(); customer.setAge(12); customer.setEmail("god@god.com"); customer.setLastName("Tom"); entityManager.persist(customer); //5. 提交事务 transaction.commit(); //6. 关闭 EntityManager entityManager.close(); //7. 关闭 EntityManagerFactory entityManagerFactory.close(); } }
https://github.com/Genesisxu/JPADemo/commits/master
标签:
原文地址:http://www.cnblogs.com/God-/p/5838355.html