jpa入门(使用hibernate)
来源:互联网 发布:对网络直播的看法论文 编辑:程序博客网 时间:2024/06/05 10:08
新建工程,加入jar包,如图:
User:
package com.jpa;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.SequenceGenerator;@Entity(name="TB_USER")public class User {@Id@GeneratedValue(strategy=GenerationType.SEQUENCE)@SequenceGenerator(name="SEQ_USER")private int id;@Column(name="USER_AGE")private int age;@Column(name="USER_NAME")private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User [id=" + id + ", age=" + age + ", name=" + name + "]";}}
persistence.xml:
<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="orclJPA"><properties><property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /><property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" /><property name="hibernate.connection.username" value="HIBERNATE" /><property name="hibernate.connection.password" value="HIBERNATE" /><property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:ORCL" /><property name="hibernate.max_fetch_depth" value="3" /><property name="hibernate.hbm2ddl.auto" value="update" /></properties></persistence-unit></persistence>
建表SQL:
CREATE TABLE TB_USER( ID INTEGER PRIMARY KEY, USER_NAME VARCHAR2(20) NOT NULL, USER_AGE INTEGER NOT NULL);
CREATE SEQUENCE SEQ_USERINCREMENT BY 1START WITH 1NOMAXVALUENOCYCLECACHE 10;
CREATE OR REPLACE TRIGGER TR_USER BEFORE INSERT ON TB_USER FOR EACH ROWBEGIN SELECT SEQ_USER.NEXTVAL INTO :NEW.ID FROM DUAL;END;
测试代码:
package com.jpa;import java.util.Random;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import junit.framework.TestCase;public class TestJPA extends TestCase {private static final Random random = new Random();private static final String persistenceUnitName = "orclJPA";private static EntityManagerFactory emf;private static EntityManager em;@Overrideprotected void setUp() throws Exception {emf = Persistence.createEntityManagerFactory(persistenceUnitName);emf.createEntityManager();em = emf.createEntityManager();}public void testSave() {em.getTransaction().begin();User user = new User();user.setAge(random.nextInt(100));user.setName("user" + random.nextInt(100));em.persist(user);em.getTransaction().commit();}public void testFind() {em.getTransaction().begin();User user = em.find(User.class, 1);System.out.println(user);}public void testUpdate() {em.getTransaction().begin();User user = em.find(User.class, 1);user.setName("user" + random.nextInt(100));em.getTransaction().commit();}public void testRemove() {em.getTransaction().begin();User user = em.find(User.class, 3);em.remove(user);em.getTransaction().commit();}@Overrideprotected void tearDown() throws Exception {em.close();emf.close();}}
0 0
- jpa入门(使用hibernate)
- hibernate-使用data jpa
- JPA入门使用
- 混合使用JPA和Hibernate
- 使用JPA 注解的Hibernate
- JPA在hibernate中的使用
- Hibernate Annotation使用实例---JPA
- Hibernate使用JPA的方式
- hibernate-jpa拦截器使用
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- JPA入门例子(采用JPA的hibernate实现版本)
- 软件架构师的特点
- mac下配置cocos2d-x android项目环境并使Eclipse模拟器正常编译运行
- Ubuntu 完美安装Sublime Text 2
- 加入了windows Domain域的Windows 2008如何自动登录
- Linux内核学习总结(附录linux协议栈函数调用图)
- jpa入门(使用hibernate)
- 《STL源码剖析》——迭代器(iterators)概念与traits编程技法(二)
- log4j(一)
- linux移植篇二
- CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)
- linux shell 无密码登陆
- linux man 手册各个章节的意义和用法
- poj2955
- setsockopt函数使用