Detailed description of the second-level cache of the Hibernate annotation method, detailed description of the hibernate Annotation

Source: Internet
Author: User

Detailed description of the second-level cache of the Hibernate annotation method, detailed description of the hibernate Annotation

Detailed description of the second-level cache in the Hibernate annotation Method

By default, hibernate supports level-1 caching, that is, session-level caching. By default, level-2 caching is not supported, that is, sessionFactory-level caching, the second-level cache generally considers it less, unless the efficiency requirement is very high,

At this time, if one of our entities needs to use the cache between sessions in multiple sessions, we can configure the second-level cache!
When reading the document, we can configure the file in persistence. xml, but I usually don't need this file, so I can directly use annotations!

You must enable the second-level cache function before using the second-level cache function. Otherwise, the second-level cache function cannot be used;

The specific method is to add

<property name="cache.use_second_level_cache">true</property> <property name="cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

You also need to import the ehcache. jar package

Generally, if the class needs to be used in the second-level cache, the annotation @ cacheable is added to the class, which indicates that the class supports second-level cache. In addition, you can also use the @ cache annotation to select a cache policy. There are four main cache policies:

  • Read-only
  • Read-write
  • Nonstrict-read-write
  • Transactional

The usage is similar to this:

@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

In addition, the @ cache annotation can also be used to include the collection class attributes of other entity classes, so that the set will also be cached!

Example:

// Entity class @ cacheable @ Entity @ Cacheable @ Cache (usage = CacheConcurrencyStrategy. NONSTRICT_READ_WRITE) public class Forest {...} // collection class @ cache @ onetoetype (cascade = CascadeType. ALL, fetch = FetchType. EAGER) @ JoinColumn (name = "CUST_ID") @ Cache (usage = CacheConcurrencyStrategy. NONSTRICT_READ_WRITE) public SortedSet <Ticket> getTickets () {return tickets ;}

Test:

@ Test public void testFind () {// Model1 uses the second-level cache. Here, only one SQL statement Session = factory is issued. openSession (); Model1 model = (Model1) session. get (Model1.class, 1); System. out. println (model. getId (); Session session2 = factory. openSession (); Model1 model2 = (Model1) session2.get (Model1.class, 1); System. out. println (model2.getId (); session. close (); session2.close () ;}@ Test public void testFind2 () {// Model2 does not perform secondary cache, so here two SQL statements are issued: Session session = factory. openSession (); Model2 model = (Model2) session. get (Model2.class, 1); System. out. println (model. getId (); Session session2 = factory. openSession (); Model2 model2 = (Model2) session2.get (Model2.class, 1); System. out. println (model2.getId (); session. close (); session2.close ();}

@ Cache is used for collection class attributes:

@ Test public void testFind4 () {// before you add a secondary Cache annotation @ Cache to models in Model1, // when models is used for the same object in different sessions, multiple SQL statements will be issued. // After @ Cache is added, only one SQL statement Session = factory will be issued below. openSession (); Model1 model = (Model1) session. get (Model1.class, 1); List <Model2> models = model. getModels (); for (Model2 mode2: models) {System. out. println (mode2.getId ();} Session session2 = factory. openSession (); Model1 model1 = (Model1) session2.get (Model1.class, 1); List <Model2> models2 = model1.getModels (); for (Model2 model2: models2) {System. out. println (model2.getId ();} session. close (); session2.close ();}
@OneToMany(mappedBy="model1")   @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)   public List<Model2> getModels() {     return models;   } 

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.