国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

JPA學(xué)習(xí)筆記【二】【helloworld】

2019-11-11 04:27:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

使用JPA持久化對(duì)象的步驟

此處有個(gè)小問(wèn)題,在創(chuàng)建JPA工程的時(shí)候,提示下圖這樣一個(gè)問(wèn)題,at least one user library must be selected,不解決則無(wú)法創(chuàng)建這個(gè)工程,網(wǎng)上搜索了下,參考http://www.cnblogs.com/lj95801/p/5001882.html解決了此問(wèn)題。

persistence.xml代碼

```

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.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_2_0.xsd"><persistence-unit name="jpa-1"><!-- 配置使用什么ORM產(chǎn)品來(lái)做作為JPA的實(shí)現(xiàn) 1.實(shí)際上配置的是javax.persistence.spi.PersistencePRovider接口的實(shí)現(xiàn)類 2.若JPA項(xiàng)目中只有一個(gè)JPA實(shí)現(xiàn)產(chǎn)品,則也可以不配置該節(jié)點(diǎn) --><provider>org.hibernate.ejb.HibernatePersistence</provider><class>com.abcd.helloworld.Customer</class><properties><!-- 鏈接數(shù)據(jù)庫(kù)的基本信息 --><property name="javax.persistence.jdbc.driver" value="com.MySQL.jdbc.Driver" /><property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa" /><property name="javax.persistence.jdbc.user" value="test" /><property name="javax.persistence.jdbc.passWord" value="1234" /><!-- 配置JPA實(shí)現(xiàn)產(chǎn)品的基本屬性,配置Hibernate的基本屬性 --><property name="hibernate.hbm2ddl.auto" value="update" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/></properties></persistence-unit></persistence>

```

測(cè)試代碼

```

//1.創(chuàng)建EntityManagerFactoryString persistenceUnitName = "jpa-1";EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);//2.創(chuàng)建 EntityManageerEntityManager manager = factory.createEntityManager();//3.開(kāi)啟事務(wù)EntityTransaction traction =  manager.getTransaction();traction.begin();System.out.println(traction);//4.進(jìn)行持久化操作Customer customer = new Customer();customer.setAge(11);customer.setEmail("abc@abcd.com");customer.setLastName("tom");manager.persist(customer);//5.提交事務(wù)traction.commit();//6.關(guān)閉EntityManagermanager.close();//7.關(guān)閉EntityManagerFactoryfactory.close();

```


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 涞水县| 南澳县| 滦平县| 大丰市| 临清市| 新邵县| 江陵县| 杭锦后旗| 罗山县| 合山市| 铁岭县| 澄江县| 奉化市| 河池市| 越西县| 固原市| 辉南县| 嵊州市| 舞阳县| 英山县| 庆安县| 旬邑县| 迭部县| 饶平县| 平和县| 黄山市| 凌源市| 高要市| 呼和浩特市| 大连市| 虞城县| 吴堡县| 乐都县| 峨眉山市| 塔河县| 读书| 尉犁县| 迭部县| 金乡县| 固原市| 介休市|