27 lazy loading and immediate loading

Question 1: When we query the customer would want to check out the contact?

Analysis: If we do not check it, when in use but also to write their own code, call the method to query. If we check out, when not in use will go to waste server memory.

Resolution: The idea of ​​using lazy loading. By way of configuration to set when we need to launch a real inquiry.

/ ** 
     * Add comment @OneToMany client object fetch attributes 
     * FetchType.EAGER: load immediately 
     * FetchType.LAZY: lazy loading 
     * / 
    @OneToMany (the mappedBy = "the Customer", fetch = FetchType.LAZY)
     Private the Set <LINKman > = linkMans new new HashSet <> (0);

Question 2: When we query the contact, the customer would want to check out?

Analysis: For example: query contact details, will certainly take a look at your customers that contact. If we do not check it, when in use but also to write their own code, call the method to query. If we find out if an object does not consume too much memory. And in most cases we are to use.

Solution: using ideas immediately loaded. Is set as a configuration, as long as a query from the entity list, the entity object at the same time put the primary table to check out

/ ** 
     * Add fetch attribute in @ManyToOne annotation contact object 
     * FetchType.EAGER: load immediately 
     * FetchType.LAZY: lazy loading 
     * / 
    @ManyToOne (targetEntity . = The Customer class , fetch = FetchType.EAGER) 
    @JoinColumn ( name = "the cust_id", the referencedColumnName = "the cust_id" )
     Private the Customer Customer;

 

 

 

Guess you like

Origin www.cnblogs.com/zhaochengf/p/12127937.html