JPA 教程 实例

实体类:

 

Java代码   收藏代码
  1. package test.entityjpa;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.Id;  
  6. import javax.persistence.Table;  
  7.   
  8. /** 
  9.  * JesonTest entity. 
  10.  *  
  11.  * @author MyEclipse Persistence Tools 
  12.  */  
  13. @Entity  
  14. @Table(name = "JESON_TEST")  
  15. public class JesonTest implements java.io.Serializable {  
  16.   
  17.     // Fields  
  18.     private Integer userid;  
  19.     private String name;  
  20.     private String password;  
  21.   
  22.     // Constructors  
  23.     /** default constructor */  
  24.     public JesonTest() {  
  25.     }  
  26.   
  27.     /** minimal constructor */  
  28.     public JesonTest(Integer userid) {  
  29.         this.userid = userid;  
  30.     }  
  31.   
  32.     /** full constructor */  
  33.     public JesonTest(Integer userid, String name, String password) {  
  34.         this.userid = userid;  
  35.         this.name = name;  
  36.         this.password = password;  
  37.     }  
  38.   
  39.     // Property accessors  
  40.     @Id  
  41.     @Column(name = "USERID", unique = true, nullable = false, insertable = true, updatable = true, precision = 20, scale = 0)  
  42.     public Integer getUserid() {  
  43.         return this.userid;  
  44.     }  
  45.   
  46.     public void setUserid(Integer userid) {  
  47.         this.userid = userid;  
  48.     }  
  49.   
  50.     @Column(name = "NAME", unique = false, nullable = true, insertable = true, updatable = true)  
  51.     public String getName() {  
  52.         return this.name;  
  53.     }  
  54.   
  55.     public void setName(String name) {  
  56.         this.name = name;  
  57.     }  
  58.   
  59.     @Column(name = "PASSWORD", unique = false, nullable = true, insertable = true, updatable = true, length = 10)  
  60.     public String getPassword() {  
  61.         return this.password;  
  62.     }  
  63.   
  64.     public void setPassword(String password) {  
  65.         this.password = password;  
  66.     }  
  67.   
  68. }  

 配置文件:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
  5.     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
  6.     version="1.0">  
  7.   
  8.     <persistence-unit name="jpaDemo" transaction-type="RESOURCE_LOCAL">  
  9.         <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  10.   
  11.         <!-- 这里的class指向我们的pojo类 -->  
  12.         <class>test.entityjpa.JesonTest</class>  
  13.         <exclude-unlisted-classes>true</exclude-unlisted-classes>  
  14.   
  15.         <properties>  
  16.             <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />  
  17.             <property name="hibernate.connection.url" value="jdbc:oracle:thin:@i18nserver:1521:i18n" />  
  18.             <property name="hibernate.connection.username" value="wpeng_8g" />  
  19.             <property name="hibernate.connection.password" value="exigen" />  
  20.             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />  
  21.             <property name="hibernate.show_sql" value="true" />  
  22.             <property name="minPoolSize" value="5" />  
  23.             <property name="initialPoolSize" value="10" />  
  24.             <property name="maxPoolSize" value="200" />  
  25.             <property name="idleConnectionTestPeriod" value="120" />  
  26.             <property name="acquireIncrement" value="10" />  
  27.             <property name="checkoutTimeout" value="3600" />  
  28.             <property name="numHelperThreads" value="4" />  
  29.             <property name="maxStatements" value="400" />  
  30.             <property name="maxStatementsPerConnection" value="20" />  
  31.             <property name="maxIdleTime" value="180" />  
  32.             <property name="acquireRetryAttempts" value="30" />  
  33.         </properties>  
  34.     </persistence-unit>  
  35. </persistence>  

 测试类:

Java代码   收藏代码
  1. package test.entityjpa;  
  2.   
  3. import javax.persistence.EntityManager;  
  4. import javax.persistence.EntityManagerFactory;  
  5. import javax.persistence.EntityTransaction;  
  6. import javax.persistence.Persistence;  
  7.   
  8. public class TestCase {  
  9.   
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpaDemo");  
  13.         EntityManager manager =  factory.createEntityManager();  
  14.         EntityTransaction tran = manager.getTransaction();  
  15.         tran.begin();  
  16.         JesonTest ower = new JesonTest();  
  17.         ower.setUserid(1);  
  18.         ower.setPassword("exigen");  
  19.         ower.setName("wpeng_8g");  
  20.         manager.persist(ower);  
  21.         tran.commit();  
  22.         System.out.println(ower.getName());  
  23.     }  
  24.   
  25. }  
 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值