Spring数据 - 查询JOIN验证错误 - 查询方法

时间:2017-02-06 02:47:19

标签: sql spring validation spring-boot spring-data-jpa

我正在尝试使用Spring数据执行3表连接查询,并且我遇到了查询验证问题。

我得到的确切错误是:

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.somethinng.domain.subscriberCategoriesRepository.findByJoin()!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:144)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:212)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 39 more

在数据库方面,这是我的三个表

create table subscribers(
id int auto_increment primary key,
email varchar(255),
unique (email));


create table categories ( 
id int auto_increment primary key,
source varchar(255) not null,
description varchar(255) not null);

create table subscriberCategories(
subscriber int not null,
source int not null,
primary key (subscriber,source),  -- prevents dupes
constraint `fk_2user` foreign key (subscriber) references       subscribers(id));

这是我的POJO和存储库

订户

@Entity
@Getter
@Setter
public class Subscribers {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotNull
private String email;

public Subscribers() { }

public Subscribers(Integer id) {
    this.id = id;
}

public Subscribers(String email, String description) {
    this.email = email;
}
}

分类

@Entity
@Getter
@Setter
public class Categories {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotNull
private String description;

@NotNull
private String source;

public Categories() { }

public Categories(Integer id) {
    this.id = id;
}

public Categories(String source, String description) {
    this.source = source;
    this.description = description;
}
}

SubscriberCategories

@Entity
@Getter
@Setter
public class subscriberCategories {

@Id
@Column(name = "subscriber")
private Integer id;

@NotNull
private Integer source;

public subscriberCategories() {
}

public subscriberCategories(Integer subscriberId) {
    this.id = subscriberId;
}

public subscriberCategories(Integer source, Integer subscriberId) {
    this.source = source;
    this.id = subscriberId;
}
}

存储库

SubscriberRepository

@Repository
public interface SubscriberRepository extends  CrudRepository<Subscribers, Integer> {


}

CategoriesRepository

@Repository
public interface CategoriesRepository extends CrudRepository<Categories, Integer> {
List<Categories> findById(Integer id);
Long deleteBySource(String source);
List<Categories> findBySource(String source);
}

subscriberCategoriesRepository

@Transactional
@Repository
public interface subscriberCategoriesRepository extends  CrudRepository<subscriberCategories, Integer> {

@Query(value = "SELECT DISTINCT s.email as Subscriber, c.source as Source from Subscribers s" +
        "Inner Join subscriberCategories sc on s.id = sc.subscriber" +
        "Inner Join Categories c on sc.subscriber = s.id where s.email = 'xxxxx@gmail.com'")
List<subscriberCategories> findByJoin();

最后我有以下单元测试,当我运行它时抛出错误

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableJpaRepositories(basePackageClasses = subscriberCategories.class)
@Transactional
public class subscriberCategoriesTest {

@Autowired
subscriberCategoriesRepository subscriberCategoriesRepository;

@Test
public void testLoadCategories() {
    List<subscriberCategories> subscriberCategories = (List<subscriberCategories>) subscriberCategoriesRepository.findByJoin();
    assertEquals("Should contain something", 1, subscriberCategories.size());
}

如果有人帮我一把,我真的很感激

由于

0 个答案:

没有答案