03 JPA的CURD

常用的API

Find:根据id查询

@Test
public void testFind() {
    //1、通过工具类获取entityManager
    EntityManager entityManager = JpaUtils.getEntityManagerFactory();
    //2、开启事物
    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    //4、完成增删改查操作
    Customer customer = entityManager.find(Customer.class, 1L);
    System.out.println(customer);
    //5、提交事物(回滚事务)
    entityTransaction.commit();
    //6、释放资源
    entityManager.close();
}

日志输出:

Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Customer{custId=1, custName='xx2', custSource='null', custIndustry='新科技', custLevel='null', custAddress='null', custPhone='null'}

getReference:根据id查询

public void testFind() {
    //1、通过工具类获取entityManager
    EntityManager entityManager = JpaUtils.getEntityManagerFactory();
    //2、开启事物
    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    //4、完成增删改查操作
    Customer customer = entityManager.getReference(Customer.class, 1L);
    System.out.println(customer);
    //5、提交事物(回滚事务)
    entityTransaction.commit();
    //6、释放资源
    entityManager.close();
}

日志输出:

Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Customer{custId=1, custName='xx2', custSource='null', custIndustry='新科技', custLevel='null', custAddress='null', custPhone='null'}

getReference与find区别?

getReference方法:

1、获取的对象是一个动态代理对象03 JPA的CURD

2、调用getReference方法不会立即发送sql语句查询数据库(懒加载 机制)

  • 当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库

find方法:

1、查询的对象返回客户对象本身![img](file:///private/var/folders/_m/9k7sm4jj3jx3hx4v3n3bk9_80000gn/T/WizNote/6b8410aa-ce30-4350-858f-a43e24452d90/index_files/34b7a960-5906-4c9e-91dd-0e7031c3cb2a.jpg)

2、在调用find方法的时候,就会发送sql进行查询

remove:删除一条数据

@Test
public void testRemove() {
    //1、通过工具类获取entityManager
    EntityManager entityManager = JpaUtils.getEntityManagerFactory();
    //2、开启事物
    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    //4、完成增删改查操作
    Customer customer = entityManager.find(Customer.class, 1L);
    entityManager.remove(customer);
    //5、提交事物(回滚事务)
    entityTransaction.commit();
    //6、释放资源
    entityManager.close();
}

日志输出:

Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Hibernate: delete from cst_customer where cust_id=?

update:更新数据

@Test
    public void testUpdate() {
        //1、通过工具类获取entityManager
        EntityManager entityManager = JpaUtils.getEntityManagerFactory();
        //2、开启事物
        EntityTransaction entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
        //4、完成增删改查操作
        Customer customer = entityManager.getReference(Customer.class, 2L);
        customer.setCustIndustry("It教育");
        entityManager.merge(customer);
        //5、提交事物(回滚事务)
        entityTransaction.commit();
        //6、释放资源
        entityManager.close();
    }

日志输出:

Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
Hibernate: update cst_customer set cust_address=?, cust_industry=?, cust_level=?, cust_name=?, cust_phone=?, cust_source=? where cust_id=?
上一篇:学习Hibernate5这一篇就够了


下一篇:Sql Server 高级特性