Skip to content

fix: Error querying entity with @IdClass #176 #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.SingularAttribute;

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

SingularAttribute<?,?> idAttribute = entityType.getId(Object.class);

query.select(cb.count(root.get(idAttribute.getName())));
query.select(cb.count(root));

List<Predicate> predicates = field.getArguments().stream()
.map(it -> getPredicate(cb, root, null, environment, it))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.introproventures.graphql.jpa.query.idclass;

import static org.assertj.core.api.Assertions.assertThat;

import javax.persistence.EntityManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = "spring.datasource.data=EntityWithIdClassTest.sql")
@TestPropertySource({"classpath:hibernate.properties"})
public class EntityWithIdClassTest {

@SpringBootApplication
static class Application {
@Bean
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) {
return new GraphQLJpaExecutor(graphQLSchemaBuilder.build());
}

@Bean
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
return new GraphQLJpaSchemaBuilder(entityManager)
.name("IdClassCompsiteKeysTest");
}
}

@Autowired
private GraphQLExecutor executor;

@Test
public void querySingularEntityWithIdClass() {
//given
String query = "query {" +
" Account(" +
" accountNumber: \"1\"" +
" accountType: \"Savings\"" +
" )" +
" {" +
" accountNumber" +
" accountType" +
" description" +
" }" +
"}";

String expected = "{Account={accountNumber=1, accountType=Savings, description=Saving account record}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryEntityWithIdClass() {
//given
String query = "query {" +
" Accounts {" +
" total" +
" pages" +
" select {" +
" accountNumber" +
" accountType" +
" description" +
" }" +
" }" +
"}";

String expected = "{Accounts={total=2, pages=1, select=["
+ "{accountNumber=1, accountType=Savings, description=Saving account record}, "
+ "{accountNumber=2, accountType=Checking, description=Checking account record}"
+ "]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryEntityWithIdClassWhereCriteriaExpression() {
//given
String query = "query {" +
" Accounts(" +
" where: {" +
" accountNumber: {" +
" EQ: \"1\"" +
" }" +
" accountType: {" +
" EQ: \"Savings\"" +
" }" +
" })" +
" {" +
" total" +
" pages" +
" select {" +
" accountNumber" +
" accountType" +
" description" +
" }" +
" }" +
"}";

String expected = "{Accounts={total=1, pages=1, select=["
+ "{accountNumber=1, accountType=Savings, description=Saving account record}"
+ "]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.introproventures.graphql.jpa.query.idclass.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

import lombok.Data;

@Entity
@IdClass(AccountId.class)
@Data
public class Account {

@Id
private String accountNumber;

@Id
private String accountType;

private String description;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.introproventures.graphql.jpa.query.idclass.model;

import java.io.Serializable;

import lombok.Data;

@Data
public class AccountId implements Serializable {

private static final long serialVersionUID = 1L;

private String accountNumber;
private String accountType;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Json entity

insert into ACCOUNT(account_number, account_type, description) values
('1', 'Savings', 'Saving account record'),
('2', 'Checking', 'Checking account record');