Some methods of 16_EntityManager: find,getReference,persist,remove,merge

1.find method

First, override the toString method in the entity class

@Override
public String toString() {
    return "Customer{" +
            "id=" + id +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            ", age=" + age +
            ", createdTime=" + createdTime +
            ", birth=" + birth +
            '}';
}

Data with this object in the database

Then, write the test method of find

/**
 * @author: jinbang
 * @create: 2019/8/13 13:26
 */

public class JpaCrudTest {
    private EntityManager entityManager;
    private EntityTransaction entityTransaction;

    @Before//Explain that the method is executed before the test method
    public void init(){
        //JpaUtils is a tool class I wrote myself
        entityManager = JpaUtils.getEntityManager();
        entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
    }

    @After//Explain that the method is executed after the test method
    public void destroy(){
        entityTransaction.commit();
        entityManager.close();
    }

    //get method similar to session in hibernate
    @Test
    public void testFind(){
        Customer customer = entityManager.find(Customer.class, 1);
        System.out.println("---------------------------------------------");
        System.out.println(customer);
    }
    

Result: Explain that the sql statement was initialized when the method was called

2.getReference method

Test code:

    //load method similar to Session in hibernate
    @Test
    public void testGetReference(){
        Customer customer = entityManager.getReference(Customer.class, 1);
        System.out.println("---------------------------------------------");
        System.out.println(customer);

Result: This method does not execute sql immediately when it is used, and sql statements are executed only when the results returned by this method are needed.

3.persist method

Similar to hibernate's save method, objects change from temporary state to persistent state

Unlike hibernate's save method, if the object has an id, the insert operation cannot be performed and an exception will be thrown

4. remote method

Test code:

 //Similar to hibernate's delete method, the record corresponding to the object is removed from the database.
    //Note: This method can only remove persistent objects, while hibernate's delete method can also remove free objects.
    @Test
    public void testRemove(){
     //   Customer customer = new Customer(); this is an operation to delete free objects, not jpa, for hibernate
     //   customer.setId(2);

        Customer customer = entityManager.find(Customer.class, 1);
        entityManager.remove(customer);

    }

Console:

As a result, the record with id 1 is removed

5.merge method

5.1. Importing Temporary Objects

Test code:

 /**
     * Overall, a saveOrUpdate method similar to hibernate Session
     */
    //1. If the incoming object is a temporary object
    @Test
    public void testMerge(){
        Customer customer = new Customer();
        customer.setAge(15);
        customer.setCreatedTime(new Date());
        customer.setBirth(new Date());
        customer.setLastName("Mac");
        customer.setEmail("mac@163.com");

        Customer customer2 = entityManager.merge(customer);
        System.out.println("customer#Id:"+customer.getId());
        System.out.println("customer2#Id:"+customer2.getId());

    }

Console:

5.2. Free object + nonexistence

 //1. If the incoming object is a free object, there is no data with the id of 100 in the database.
    @Test
    public void testMerge2(){
        Customer customer = new Customer();
        customer.setAge(15);
        customer.setCreatedTime(new Date());
        customer.setBirth(new Date());
        customer.setLastName("Mac");
        customer.setEmail("mac@163.com");

        customer.setId(100);

        Customer customer2 = entityManager.merge(customer);
        System.out.println("customer#Id:"+customer.getId());
        System.out.println("customer2#Id:"+customer2.getId());

    }

Console: The last thing to do is insert

5.3. Free + Existence

 //1. If the incoming object is a free object, there is data with id 3 in the database.
    @Test
    public void testMerge3(){
        Customer customer = new Customer();
        customer.setAge(18);
        customer.setCreatedTime(new Date());
        customer.setBirth(new Date());
        customer.setLastName("abc");
        customer.setEmail("abc@163.com");

        customer.setId(3);

        Customer customer2 = entityManager.merge(customer);
        System.out.println("customer#Id:"+customer.getId());
        System.out.println("customer2#Id:"+customer2.getId());

    }

Console: Execute update

r.merge(customer);
System.out.println("customer#Id:"+customer.getId());
System.out.println("customer2#Id:"+customer2.getId());

}
Console: Execute update


[Insert picture description here] (https://img-blog.csdnimg.cn/20190814112107804.png?X-oss-process=image/watermark, type_ZmFuZ3poZW5naGVpdGk, shadow_10, text_aHR0cHM6Ly9ib9nLmNZG4mV0L1JpY2t5X01vby2g=, color_FF, t_70)

Keywords: Hibernate Database Mac Session

Added by echion on Sun, 06 Oct 2019 19:29:08 +0300