Weird Behavior of Cache with fetchmode join and scroll-iterator
Description
Attachments
- 28 Oct 2015, 08:58 AM
Activity
Jan Schatteman July 11, 2024 at 5:07 PM
This issue has been rejected since the Hibernate legacy Criteria API was deprecated in 5.x and removed in 6.0. There will be no further development for it.
Steve Ebersole October 29, 2015 at 5:01 AM
The legacy Hibernate Criteria API is considered softly deprecated. Meaning it is not high on my todo list to track down problems in it. The best way to get this resolved is to investigate this yourself and either pinpoint where this breaks down or even submit a patch/PR fixing it.
Karolis Kleiza October 28, 2015 at 9:00 AM
Hi Steve,
I verified, that this issue still exists in Hibarnate 5.x. I attached the Test Case to the Ticket.
Karolis
Steve Ebersole October 28, 2015 at 3:25 AM
As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.
Steve Ebersole October 27, 2015 at 7:15 PM
This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.
For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/
Hi guys,
i found out a weird behavior with the collectionsCache when I overwrite the Fetchmode in a Criteria-query to JoinMode and use the scroll-iterator. It seems like the query builds every single child as a bag and puts it into the second-level-collection-cache.
It seems not to act like an O/R-Mapper. Such Join-Statements should be grouped or at least not corrupt the cache.
Background: I want to preheat the Secondlevel-cache with just one statement. i get rid of the ripple loading.
example
City.java:
import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import javax.persistence.*; import java.util.ArrayList; import java.util.List; @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class City { @Id @GeneratedValue private Long id; private String name; @OneToMany(fetch = FetchType.LAZY, mappedBy = "city") @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) private List<Citizen> citizens = new ArrayList<Citizen>(); public City(String name) { this.name = name; } public City() { } public List<Citizen> getCitizens() { return citizens; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Citizen.java:
import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import javax.persistence.*; @Entity @Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Citizen { @Id @GeneratedValue private Long id; private String name; @ManyToOne private City city; public Citizen(String name) { this.name = name; } public Citizen() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public City getCity() { return city; } public void setCity(City city) { this.city = city; } }
the TestCase:
import junit.framework.Assert; import org.hibernate.*; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Restrictions; import org.junit.Before; import java.util.List; public class FetchModeTest { private SessionFactory factory; @Before public void setUp() throws Exception { // just create 2 cities with 2 citizens in both cities factory = new Configuration().configure( "hibernate.cfg.xml").addAnnotatedClass(Citizen.class).addAnnotatedClass(City.class) .buildSessionFactory(); Session session = factory.openSession(); City berlin = new City("Berlin"); session.save(berlin); Citizen hans = new Citizen("Hans"); hans.setCity(berlin); session.save(hans); berlin.getCitizens().add(hans); Citizen peter = new Citizen("Peter"); peter.setCity(berlin); session.save(peter); berlin.getCitizens().add(peter); session.save(berlin); session.flush(); City hamburg = new City("Hamburg"); session.save(hamburg); Citizen horst = new Citizen("Horst"); horst.setCity(hamburg); session.save(horst); Citizen anne = new Citizen("Anne"); anne.setCity(hamburg); session.save(anne); hamburg.getCitizens().add(horst); hamburg.getCitizens().add(anne); session.close(); factory.getCache().evictEntityRegions(); //clear 2-level-cache factory.getCache().evictCollectionRegions(); //clear 2-level-cache collections } @org.junit.Test //PASSED!!! public void testWithJoinAndList() throws Exception { preHeatCacheWithJoinAndList(); Session newSession = factory.openSession(); City berlin = (City) newSession.createCriteria(City.class).add(Restrictions.eq("name", "Berlin")).uniqueResult(); Assert.assertEquals(2, berlin.getCitizens().size()); newSession.close(); } @org.junit.Test //FAILED!!! public void testWithJoinAndScroll() throws Exception { preheatCacheWithJoinAndScroll(); Session newSession = factory.openSession(); City berlin = (City) newSession.createCriteria(City.class).add(Restrictions.eq("name", "Berlin")).uniqueResult(); Assert.assertEquals(2, berlin.getCitizens().size()); newSession.close(); } private void preHeatCacheWithJoinAndList() { Session preheatSession = factory.openSession(); List<City> list = preheatSession.createCriteria(City.class).setFetchMode("citizens", FetchMode.JOIN).list(); for (City city : list) { Hibernate.initialize(city); //citizen-bag has 2 Element } preheatSession.close(); } private void preheatCacheWithJoinAndScroll() { Session preheatSession = factory.openSession(); ScrollableResults cursor = preheatSession.createCriteria(City.class).setFetchMode("citizens", FetchMode.JOIN).scroll(); while (cursor.next()) { City city = (City) cursor.get(0); Hibernate.initialize(city); //citizen-bag has just 1 Element } preheatSession.close(); } }
thanks