Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<spring.version>4.2.0.RELEASE</spring.version>
<spring.data.jpa.version>1.8.2.RELEASE</spring.data.jpa.version>
<hibernate.version>4.3.10.Final</hibernate.version>
<hibernate.version>4.3.11.Final</hibernate.version>
<hibernate.validator.version>5.2.0.Final</hibernate.validator.version>
<jackson.version>2.6.0</jackson.version>
<javax.el.api.version>2.2.5</javax.el.api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.From;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
Expand Down Expand Up @@ -102,6 +103,29 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
}
predicate = criteriaBuilder.and(predicate, matchOneColumnPredicate);
}
// findAll method does a count query first, and then query for the actual data. Yet in the
// count query, adding a JOIN FETCH results in the following error 'query specified join
// fetching, but the owner of the fetched association was not present in the select list'
// see https://jira.spring.io/browse/DATAJPA-105
boolean isCountQuery = query.getResultType() == Long.class;
if (isCountQuery) {
return predicate;
}
// add JOIN FETCH when necessary
for (ColumnParameter column : input.getColumns()) {
if (!column.getSearchable() || !column.getData().contains(ATTRIBUTE_SEPARATOR)) {
continue;
}
String[] values = column.getData().split("\\" + ATTRIBUTE_SEPARATOR);
if (root.getModel().getAttribute(values[0])
.getPersistentAttributeType() == PersistentAttributeType.EMBEDDED) {
continue;
}
Fetch<?, ?> fetch = null;
for (int i = 0; i < values.length - 1; i++) {
fetch = (fetch == null ? root : fetch).fetch(values[i], JoinType.LEFT);
}
}
return predicate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Environment;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -60,4 +62,10 @@ public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLExcepti

return bean;
}

@Bean
public SessionFactory sessionFactory() throws SQLException {
return ((HibernateEntityManagerFactory) entityManagerFactory().getObject()).getSessionFactory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.hibernate.SessionFactory;
import org.hibernate.stat.Statistics;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +24,9 @@ public class LessonRepositoryTest {
@Autowired
private LessonRepository lessonRepository;

@Autowired
private SessionFactory sessionFactory;

@Test
public void testThroughTwoManyToOneRelationships() {
DataTablesInput input = getBasicInput();
Expand All @@ -41,6 +46,22 @@ public void testThroughTwoManyToOneRelationships() {
assertEquals(7, output.getRecordsTotal());
}

@Test
public void testEagerLoading() {
DataTablesInput input = getBasicInput();

Statistics statistics = sessionFactory.getStatistics();
statistics.setStatisticsEnabled(true);
DataTablesOutput<Lesson> output = lessonRepository.findAll(input);
assertEquals("CourseTypeA", output.getData().get(0).getCourse().getType().getName());
statistics.setStatisticsEnabled(false);

// there should be only three executed queries : count unfiltered, count filtered and actual
// data (with FETCH JOIN)
assertEquals(3, statistics.getPrepareStatementCount());
assertEquals(7 + 3 + 2, statistics.getEntityLoadCount());
}

/**
*
* @return basic input parameters
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.logger.org.springframework=INFO
log4j.logger.org.springframework=INFO
log4j.logger.org.hibernate.SQL=DEBUG