Skip to content

Commit 50df533

Browse files
authored
fix: Error querying entity with @IdClass #176 (#179)
1 parent 82be5d5 commit 50df533

File tree

5 files changed

+173
-4
lines changed

5 files changed

+173
-4
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaQueryDataFetcher.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javax.persistence.criteria.Predicate;
3131
import javax.persistence.criteria.Root;
3232
import javax.persistence.metamodel.EntityType;
33-
import javax.persistence.metamodel.SingularAttribute;
3433

3534
import graphql.language.Argument;
3635
import graphql.language.BooleanValue;
@@ -187,9 +186,7 @@ private TypedQuery<Long> getCountQuery(DataFetchingEnvironment environment, Fiel
187186
CriteriaQuery<Long> query = cb.createQuery(Long.class);
188187
Root<?> root = query.from(entityType);
189188

190-
SingularAttribute<?,?> idAttribute = entityType.getId(Object.class);
191-
192-
query.select(cb.count(root.get(idAttribute.getName())));
189+
query.select(cb.count(root));
193190

194191
List<Predicate> predicates = field.getArguments().stream()
195192
.map(it -> getPredicate(cb, root, null, environment, it))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.introproventures.graphql.jpa.query.idclass;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import javax.persistence.EntityManager;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.test.context.TestPropertySource;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
17+
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
18+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
19+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
23+
properties = "spring.datasource.data=EntityWithIdClassTest.sql")
24+
@TestPropertySource({"classpath:hibernate.properties"})
25+
public class EntityWithIdClassTest {
26+
27+
@SpringBootApplication
28+
static class Application {
29+
@Bean
30+
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) {
31+
return new GraphQLJpaExecutor(graphQLSchemaBuilder.build());
32+
}
33+
34+
@Bean
35+
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
36+
return new GraphQLJpaSchemaBuilder(entityManager)
37+
.name("IdClassCompsiteKeysTest");
38+
}
39+
}
40+
41+
@Autowired
42+
private GraphQLExecutor executor;
43+
44+
@Test
45+
public void querySingularEntityWithIdClass() {
46+
//given
47+
String query = "query {" +
48+
" Account(" +
49+
" accountNumber: \"1\"" +
50+
" accountType: \"Savings\"" +
51+
" )" +
52+
" {" +
53+
" accountNumber" +
54+
" accountType" +
55+
" description" +
56+
" }" +
57+
"}";
58+
59+
String expected = "{Account={accountNumber=1, accountType=Savings, description=Saving account record}}";
60+
61+
//when
62+
Object result = executor.execute(query).getData();
63+
64+
// then
65+
assertThat(result.toString()).isEqualTo(expected);
66+
}
67+
68+
@Test
69+
public void queryEntityWithIdClass() {
70+
//given
71+
String query = "query {" +
72+
" Accounts {" +
73+
" total" +
74+
" pages" +
75+
" select {" +
76+
" accountNumber" +
77+
" accountType" +
78+
" description" +
79+
" }" +
80+
" }" +
81+
"}";
82+
83+
String expected = "{Accounts={total=2, pages=1, select=["
84+
+ "{accountNumber=1, accountType=Savings, description=Saving account record}, "
85+
+ "{accountNumber=2, accountType=Checking, description=Checking account record}"
86+
+ "]}}";
87+
88+
//when
89+
Object result = executor.execute(query).getData();
90+
91+
// then
92+
assertThat(result.toString()).isEqualTo(expected);
93+
}
94+
95+
@Test
96+
public void queryEntityWithIdClassWhereCriteriaExpression() {
97+
//given
98+
String query = "query {" +
99+
" Accounts(" +
100+
" where: {" +
101+
" accountNumber: {" +
102+
" EQ: \"1\"" +
103+
" }" +
104+
" accountType: {" +
105+
" EQ: \"Savings\"" +
106+
" }" +
107+
" })" +
108+
" {" +
109+
" total" +
110+
" pages" +
111+
" select {" +
112+
" accountNumber" +
113+
" accountType" +
114+
" description" +
115+
" }" +
116+
" }" +
117+
"}";
118+
119+
String expected = "{Accounts={total=1, pages=1, select=["
120+
+ "{accountNumber=1, accountType=Savings, description=Saving account record}"
121+
+ "]}}";
122+
123+
//when
124+
Object result = executor.execute(query).getData();
125+
126+
// then
127+
assertThat(result.toString()).isEqualTo(expected);
128+
}
129+
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.introproventures.graphql.jpa.query.idclass.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.IdClass;
6+
7+
import lombok.Data;
8+
9+
@Entity
10+
@IdClass(AccountId.class)
11+
@Data
12+
public class Account {
13+
14+
@Id
15+
private String accountNumber;
16+
17+
@Id
18+
private String accountType;
19+
20+
private String description;
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.introproventures.graphql.jpa.query.idclass.model;
2+
3+
import java.io.Serializable;
4+
5+
import lombok.Data;
6+
7+
@Data
8+
public class AccountId implements Serializable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private String accountNumber;
13+
private String accountType;
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Json entity
2+
3+
insert into ACCOUNT(account_number, account_type, description) values
4+
('1', 'Savings', 'Saving account record'),
5+
('2', 'Checking', 'Checking account record');

0 commit comments

Comments
 (0)