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 @@ -5,7 +5,7 @@

<groupId>com.github.darrachequesne</groupId>
<artifactId>spring-data-jpa-datatables</artifactId>
<version>2.2</version>
<version>2.3-SNAPSHOT</version>

<name>Spring Data JPA for DataTables</name>
<description>Spring Data JPA extension to work with the great jQuery plug-in DataTables (http://datatables.net/)</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Attribute.PersistentAttributeType;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -193,10 +195,16 @@ private static Expression<String> getExpression(Root<?> root, String columnData)
if (columnData.contains(ATTRIBUTE_SEPARATOR)) {
// columnData is like "joinedEntity.attribute" so add a join clause
String[] values = columnData.split("\\" + ATTRIBUTE_SEPARATOR);
if (!root.getModel().getAttribute(values[0]).isAssociation()) {
if (root.getModel().getAttribute(values[0])
.getPersistentAttributeType() == PersistentAttributeType.EMBEDDED) {
// with @Embedded attribute
return root.get(values[0]).get(values[1]).as(String.class);
}
return root.join(values[0], JoinType.LEFT).get(values[1]).as(String.class);
From<?, ?> from = root;
for (int i = 0; i < values.length - 1; i++) {
from = from.join(values[i], JoinType.LEFT);
}
return from.get(values[values.length - 1]).as(String.class);
} else {
// columnData is like "attribute" so nothing particular to do
return root.get(columnData).as(String.class);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/springframework/data/jpa/datatables/Config.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.springframework.data.jpa.datatables;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.cfg.Environment;
import org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.datatables.repository.DataTablesRepositoryFactoryBean;
Expand Down Expand Up @@ -48,6 +51,13 @@ public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLExcepti
bean.setPackagesToScan(Config.class.getPackage().getName());
bean.setDataSource(dataSource());

Properties jpaProperties = new Properties();
jpaProperties.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
jpaProperties.setProperty(Environment.HBM2DDL_IMPORT_FILES, "init.sql");
jpaProperties.setProperty(Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR,
MultipleLinesSqlCommandExtractor.class.getName());
bean.setJpaProperties(jpaProperties);

return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.springframework.data.jpa.datatables.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "course")
public class Course {

@Id
@GeneratedValue
private Long id;

private String name;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "course_type_id")
private CourseType type;

public Course() {}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public CourseType getType() {
return type;
}

public void setType(CourseType type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.springframework.data.jpa.datatables.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "course_type")
public class CourseType {

@Id
@GeneratedValue
private Long id;

private String name;

public CourseType() {}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.springframework.data.jpa.datatables.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "lesson")
public class Lesson {

@Id
@GeneratedValue
private Long id;

private String name;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "course_id")
private Course course;

public Lesson() {}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Course getCourse() {
return course;
}

public void setCourse(Course course) {
this.course = course;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.data.jpa.datatables.model;

import org.springframework.data.jpa.datatables.repository.DataTablesRepository;

public interface LessonRepository extends DataTablesRepository<Lesson, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.springframework.data.jpa.datatables.repository;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.Config;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.data.jpa.datatables.model.Lesson;
import org.springframework.data.jpa.datatables.model.LessonRepository;
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class LessonRepositoryTest {

@Autowired
private LessonRepository lessonRepository;

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

input.getColumns().get(3).getSearch().setValue("CourseTypeA");
DataTablesOutput<Lesson> output = lessonRepository.findAll(input);
assertNotNull(output);
assertNull(output.getError());
assertEquals(5, (long) output.getRecordsFiltered());
assertEquals(7, (long) output.getRecordsTotal());

input.getColumns().get(2).getSearch().setValue("CourseA-2");
output = lessonRepository.findAll(input);
assertNotNull(output);
assertNull(output.getError());
assertEquals(2, (long) output.getRecordsFiltered());
assertEquals(7, (long) output.getRecordsTotal());
}

/**
*
* @return basic input parameters
*/
private static DataTablesInput getBasicInput() {
DataTablesInput input = new DataTablesInput();
input.setDraw(1);
input.setStart(0);
input.setLength(10);
input.setSearch(new SearchParameter("", false));
input.setOrder(new ArrayList<OrderParameter>());
input.getOrder().add(new OrderParameter(0, "asc"));

input.setColumns(new ArrayList<ColumnParameter>());
input.getColumns()
.add(new ColumnParameter("id", "", true, true, new SearchParameter("", false)));
input.getColumns()
.add(new ColumnParameter("name", "", true, true, new SearchParameter("", false)));
input.getColumns()
.add(new ColumnParameter("course.name", "", true, true, new SearchParameter("", false)));
input.getColumns().add(
new ColumnParameter("course.type.name", "", true, true, new SearchParameter("", false)));

return input;
}
}
19 changes: 19 additions & 0 deletions src/test/resources/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
INSERT INTO course_type (id, name) VALUES
(1, 'CourseTypeA'),
(2, 'CourseTypeB'),
(3, 'CourseTypeC');

INSERT INTO course (id, name, course_type_id) VALUES
(1, 'CourseA-1', 1),
(2, 'CourseA-2', 1),
(3, 'CourseA-3', 1),
(4, 'CourseB-1', 2);

INSERT INTO lesson (id, name, course_id) VALUES
(1, 'LessonA-1-a', 1),
(2, 'LessonA-1-b', 1),
(3, 'LessonA-1-c', 1),
(4, 'LessonA-2-a', 2),
(5, 'LessonA-2-b', 2),
(6, 'LessonB-1-a', 4),
(7, 'LessonB-1-b', 4);