码迷,mamicode.com
首页 > 其他好文 > 详细

JPA EntiityManager.find方法

时间:2020-07-29 21:17:17      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:factor   comm   persist   类对象   缓存   tran   调用   更新   HERE   

find 方法

  • 立即加载:在调用find方法的时候,就会查找缓存,找不到立即发送sql语句查询数据库。
  • 找到返回实体类对象,可以用于remove,setter,remove之后还可以调用persist保存。
  • 找不到返回null。

find 方法测试

find 之后 setter 实现更新

@Test
public void test1() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Student s = em.find(Student.class, 1L);
    /*

    Hibernate:
        select
            student0_.sid as sid1_0_0_,
            student0_.sage as sage2_0_0_,
            student0_.sname as sname3_0_0_
        from
            Student student0_
        where
            student0_.sid=?

     */
    s.setSage(999); // ORM思想:操作Managed状态的实体类对象就相当于操作表中的记录

    tx.commit();
    /*

    Hibernate:
        update
            Student
        set
            sage=?,
            sname=?
        where
            sid=?

     */
    em.close();
    factory.close();
}

find 之后 remove 实现删除

@Test
public void test2() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Student s = em.find(Student.class, 1L);
    /*

    Hibernate:
        select
            student0_.sid as sid1_0_0_,
            student0_.sage as sage2_0_0_,
            student0_.sname as sname3_0_0_
        from
            Student student0_
        where
            student0_.sid=?

     */
    em.remove(s); // ORM思想:操作Managed状态的实体类对象就相当于操作表中的记录

    tx.commit();
    /*

    Hibernate:
        delete
        from
            Student
        where
            sid=?

     */
    em.close();
    factory.close();
}

find 之后 remove 再 persist 相当于删除后又保存

@Test
public void test() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Student s = em.find(Student.class, 1L);
    /*

    Hibernate:
        select
            student0_.sid as sid1_0_0_,
            student0_.sage as sage2_0_0_,
            student0_.sname as sname3_0_0_
        from
            Student student0_
        where
            student0_.sid=?

     */
    em.remove(s);

    em.persist(s);

    tx.commit();
    em.close();
    factory.close();
}

find 找不到返回 null

@Test
public void test2() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Student s = em.find(Student.class, 1027500L); // 找不到返回 null
    /*

    Hibernate:
        select
            student0_.sid as sid1_0_0_,
            student0_.sage as sage2_0_0_,
            student0_.sname as sname3_0_0_
        from
            Student student0_
        where
            student0_.sid=?

     */
    System.out.println(s); // null

    tx.commit();
    em.close();
    factory.close();
}

JPA EntiityManager.find方法

标签:factor   comm   persist   类对象   缓存   tran   调用   更新   HERE   

原文地址:https://www.cnblogs.com/wumingoo1/p/13398945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!