首页 > 其他 > 详细

20130118

时间:2016-01-18 22:48:17      阅读:286      评论:0      收藏:0      [点我收藏+]
技术分享
package com.hanqi.dao;

import static org.junit.Assert.*;
import java.util.*;
import javax.persistence.*;
import org.junit.*;

public class TestJPA {

    EntityManagerFactory entityManagerFactory = null;
    EntityManager entityManager = null;
    EntityTransaction transaction = null;
    
    @Before
    public void hider(){
        
        //1. 创建 EntitymanagerFactory
        String persistenceUnitName = "TestJPA";

        entityManagerFactory = 
                Persistence.createEntityManagerFactory(persistenceUnitName);
        
        //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory
        entityManager = entityManagerFactory.createEntityManager();
        
        //3. 开启事务
        transaction = entityManager.getTransaction();
        transaction.begin();
        
    }
    
    @Test
    public void testJPQL(){
        
        String jpql = "from JPANews n where n.id = ?";
        
        Query q = entityManager.createQuery(jpql);
        
//        Query q = entityManager.createNamedQuery("cx");
        
        q.setParameter(1,6);        //占位符序号从1开始
        
        List<JPANews> l = q.getResultList();
        
        System.out.println("size = " + l.size());        
                
    }
    
    @Test
    public void test() {
        
        
                //数据操作
        
        
        //保存
                JPANews jn = new JPANews();
                
                jn.setTitle("标题");
                jn.setContent("内容");
                jn.setAuthor("作者");
                jn.setCreatedate(new Date());
                
//                entityManager.persist(jn);//保存
                

                JPANews jn2 = entityManager.merge(jn);//saveOrUpdate
                
                System.out.println("jn = " + jn);
                System.out.println("jn2 = " + jn2);
                
        //查找    
                //立即加载
                JPANews jpan = entityManager.find(JPANews.class,1);
                
                jpan.setTitle("新的标题");
                
                entityManager.flush();//提交语句给数据库,事务还未提交

//                System.out.println(jpan);
                
                
/*                
                //延迟加载
                JPANews jpan2 = entityManager.getReference(JPANews.class,3);
                
                System.out.println("id = " + jpan2.getId());
                System.out.println("title = " + jpan2.getTitle());
                
                
        //删除
                entityManager.remove(jpan2);//删除
                
*/            
    }

    
    @After
    public void test1()
    {

        //5. 提交事务
        transaction.commit();
        
        //6. 关闭 EntityManager
        entityManager.close();
        
        //7. 关闭 EntityManagerFactory
        entityManagerFactory.close();


    }
    
    
}
TestJPA
技术分享
package com.hanqi.dao;

import java.util.Date;

import javax.persistence.*;

//@NamedQuery(name="cx",query="SELECT n.id from JPANews n where n.id = ?")
@Table(name="JPA_News")    //类与表之间的映射关系
@Entity        //实体类
public class JPANews {
    
    private Integer id;
    private String title;
    private String content;
    private Date createdate;
    private String author;
    
    

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    
    }
    
    @GeneratedValue(strategy=GenerationType.AUTO)        //生成主键的策略
    @Id        //指定主键
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    
    
    @Column(name="CREATE_DATE")
    @Basic
    public Date getCreatedate() {
        return createdate;
    }
    public void setCreatedate(Date createdate) {
        this.createdate = createdate;
    }
    
    
    @Override
    @Transient
    public String toString() {
        return "JPANews [id=" + id + ", title=" + title + ", content=" + content + ", createdate=" + createdate
                + ", author=" + author + "]";
    }
    
    

}
JPANews

技术分享

20130118

原文:http://www.cnblogs.com/name-hanlin/p/5140581.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!