Skip to content

Commit c2ff267

Browse files
Fix for using @Embedded class (by @wimdeblauwe)
(closes darrachequesne#11, darrachequesne#12)
1 parent d1ede31 commit c2ff267

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
.project
33
.springBeans
44
.settings/
5+
.idea
6+
*.iml
57
build/
68
target/

src/main/java/org/springframework/data/jpa/datatables/repository/DataTablesUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ private static Expression<String> getExpression(Root<?> root, String columnData)
193193
if (columnData.contains(ATTRIBUTE_SEPARATOR)) {
194194
// columnData is like "joinedEntity.attribute" so add a join clause
195195
String[] values = columnData.split("\\" + ATTRIBUTE_SEPARATOR);
196+
if (!root.getModel().getAttribute(values[0]).isAssociation()) {
197+
return root.get(values[0]).get(values[1]).as(String.class);
198+
}
196199
return root.join(values[0], JoinType.LEFT).get(values[1]).as(String.class);
197200
} else {
198201
// columnData is like "attribute" so nothing particular to do
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.springframework.data.jpa.datatables.model;
2+
3+
import javax.persistence.Embedded;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Game {
10+
11+
@Id
12+
@GeneratedValue
13+
private Integer id;
14+
15+
@Embedded
16+
private Prize prize;
17+
18+
protected Game() {}
19+
20+
public Game(Prize prize) {
21+
this.prize = prize;
22+
}
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public Prize getPrize() {
29+
return prize;
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.springframework.data.jpa.datatables.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Embeddable;
5+
import javax.validation.constraints.NotNull;
6+
7+
@Embeddable
8+
public class Prize {
9+
10+
@NotNull
11+
@Column(name = "prize_name")
12+
private String name;
13+
14+
protected Prize() {}
15+
16+
public Prize(String name) {
17+
this.name = name;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.data.jpa.datatables.repository;
2+
3+
import org.springframework.data.jpa.datatables.model.Game;
4+
5+
public interface GameRepository extends DataTablesRepository<Game, Integer> {
6+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.springframework.data.jpa.datatables.repository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import java.util.ArrayList;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.data.jpa.datatables.Config;
14+
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
15+
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
16+
import org.springframework.data.jpa.datatables.model.Game;
17+
import org.springframework.data.jpa.datatables.model.Prize;
18+
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
19+
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
20+
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration(classes = Config.class)
26+
public class GameRepositoryTest {
27+
28+
@Autowired
29+
private GameRepository gameRepository;
30+
31+
/**
32+
* Insert sample data at the beginning of all tests
33+
*/
34+
@Before
35+
public void setUp() {
36+
if (gameRepository.count() > 0)
37+
return;
38+
for (int i = 0; i < 12; i++) {
39+
Game game = new Game(new Prize("prize" + i));
40+
gameRepository.save(game);
41+
}
42+
}
43+
44+
@Test
45+
public void testWithoutFilter() {
46+
DataTablesInput input = getBasicInput();
47+
48+
DataTablesOutput<Game> output = gameRepository.findAll(input);
49+
assertNotNull(output);
50+
assertNull(output.getError());
51+
assertEquals(12, (long) output.getRecordsFiltered());
52+
assertEquals(12, (long) output.getRecordsTotal());
53+
}
54+
55+
@Test
56+
public void testSearchOnEmbeddedProperty() {
57+
DataTablesInput input = getBasicInput();
58+
59+
input.getColumns().get(1).getSearch().setValue("prize1");
60+
DataTablesOutput<Game> output = gameRepository.findAll(input);
61+
assertNotNull(output);
62+
assertNull(output.getError());
63+
assertEquals(3, (long) output.getRecordsFiltered());
64+
}
65+
66+
@Test
67+
public void testOrderOnEmbeddedProperty() {
68+
DataTablesInput input = getBasicInput();
69+
input.setOrder(new ArrayList<OrderParameter>());
70+
input.getOrder().add(new OrderParameter(1, "desc"));
71+
72+
DataTablesOutput<Game> output = gameRepository.findAll(input);
73+
assertNotNull(output);
74+
assertNull(output.getError());
75+
assertEquals(12, (long) output.getRecordsFiltered());
76+
assertEquals("prize9", output.getData().get(0).getPrize().getName());
77+
}
78+
79+
/**
80+
*
81+
* @return basic input parameters
82+
*/
83+
private static DataTablesInput getBasicInput() {
84+
DataTablesInput input = new DataTablesInput();
85+
input.setDraw(1);
86+
input.setStart(0);
87+
input.setLength(10);
88+
input.setSearch(new SearchParameter("", false));
89+
input.setOrder(new ArrayList<OrderParameter>());
90+
input.getOrder().add(new OrderParameter(0, "asc"));
91+
92+
input.setColumns(new ArrayList<ColumnParameter>());
93+
input.getColumns()
94+
.add(new ColumnParameter("id", "", true, true, new SearchParameter("", false)));
95+
input.getColumns()
96+
.add(new ColumnParameter("prize.name", "", true, true, new SearchParameter("", false)));
97+
98+
return input;
99+
}
100+
}

0 commit comments

Comments
 (0)