实体类:
- package test.entityjpa;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- /**
- * JesonTest entity.
- *
- * @author MyEclipse Persistence Tools
- */
- @Entity
- @Table(name = "JESON_TEST")
- public class JesonTest implements java.io.Serializable {
- // Fields
- private Integer userid;
- private String name;
- private String password;
- // Constructors
- /** default constructor */
- public JesonTest() {
- }
- /** minimal constructor */
- public JesonTest(Integer userid) {
- this.userid = userid;
- }
- /** full constructor */
- public JesonTest(Integer userid, String name, String password) {
- this.userid = userid;
- this.name = name;
- this.password = password;
- }
- // Property accessors
- @Id
- @Column(name = "USERID", unique = true, nullable = false, insertable = true, updatable = true, precision = 20, scale = 0)
- public Integer getUserid() {
- return this.userid;
- }
- public void setUserid(Integer userid) {
- this.userid = userid;
- }
- @Column(name = "NAME", unique = false, nullable = true, insertable = true, updatable = true)
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Column(name = "PASSWORD", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
- public String getPassword() {
- return this.password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence 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_1_0.xsd"
- version="1.0">
- <persistence-unit name="jpaDemo" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <!-- 这里的class指向我们的pojo类 -->
- <class>test.entityjpa.JesonTest</class>
- <exclude-unlisted-classes>true</exclude-unlisted-classes>
- <properties>
- <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
- <property name="hibernate.connection.url" value="jdbc:oracle:thin:@i18nserver:1521:i18n" />
- <property name="hibernate.connection.username" value="wpeng_8g" />
- <property name="hibernate.connection.password" value="exigen" />
- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
- <property name="hibernate.show_sql" value="true" />
- <property name="minPoolSize" value="5" />
- <property name="initialPoolSize" value="10" />
- <property name="maxPoolSize" value="200" />
- <property name="idleConnectionTestPeriod" value="120" />
- <property name="acquireIncrement" value="10" />
- <property name="checkoutTimeout" value="3600" />
- <property name="numHelperThreads" value="4" />
- <property name="maxStatements" value="400" />
- <property name="maxStatementsPerConnection" value="20" />
- <property name="maxIdleTime" value="180" />
- <property name="acquireRetryAttempts" value="30" />
- </properties>
- </persistence-unit>
- </persistence>
测试类:
- package test.entityjpa;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.Persistence;
- public class TestCase {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpaDemo");
- EntityManager manager = factory.createEntityManager();
- EntityTransaction tran = manager.getTransaction();
- tran.begin();
- JesonTest ower = new JesonTest();
- ower.setUserid(1);
- ower.setPassword("exigen");
- ower.setName("wpeng_8g");
- manager.persist(ower);
- tran.commit();
- System.out.println(ower.getName());
- }
- }