应用程序属性:
spring.h2.console.enabled=true
spring.h2.console.path=/console
spring.datasource.url=jdbc:h2:mem:
logging.level.org.hibernate.SQL=debug`
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.rbrt</groupId>
<artifactId>spring-data-native-query-projection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-data-native-query-projection</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!-- Springs Java Persistence API -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
数据类:
package uk.co.rbrt;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person
{
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int age;
public Person()
{
// default no-arg constructor
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
投影界面:
package uk.co.rbrt;
public interface PersonSummary
{
String getName();
int getAge();
}
存储库:
package uk.co.rbrt;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface PersonRepository extends CrudRepository<Person, Long>
{
List<PersonSummary> findAllProjectedBy();
Query(value = "SELECT name AS name, age AS age FROM Person")
List<PersonSummary> findAllProjectedJpaQuery();
@Query(value = "SELECT name AS name, age AS age FROM Person", nativeQuery = true)
List<PersonSummary> findAllProjectedNativeQuery();
PersonSummary findProjectedById(long id);
@Query(value = "SELECT name AS name, age AS age FROM Person WHERE id = ?1")
PersonSummary findProjectedByIdJpaQuery(long id);
@Query(value = "SELECT name AS name, age AS age FROM Person WHERE id = ?1", nativeQuery = true)
PersonSummary findProjectedByIdNativeQuery(long id);
}
主要课程:
package uk.co.rbrt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataNativeQueryProjectionApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataNativeQueryProjectionApplication.class, args);
}
}
但我得到了:
2017-09-17 18:15:11.242 DEBUG 8959 --- [nio-8080-exec-2] org.hibernate.SQL : SELECT name AS name, age AS age FROM Person
2017-09-17 18:15:11.246 ERROR 8959 --- [nio-8080-exec-2] o.s.d.r.w.RepositoryRestExceptionHandler : PersistentEntity must not be null!
使用Eclipse Maven Springboot项目,编译正常但我无法调试它,因为问题存在于框架中。
有人可以分享一下为什么春天会这样做吗?它应该无法识别投影界面并映射出JSON响应吗?
答案 0 :(得分:0)
以下是已更正的PersonRepository
JPA预测。
public interface PersonRepository extends CrudRepository<Person, Long>
{
@Query(value = "SELECT p.name AS name, p.age AS age FROM Person AS p")
List<PersonSummary> findAllProjectedJpaQuery();
@Query(value = ""SELECT p.name AS name, p.age AS age FROM Person AS p"", nativeQuery = true)
List<PersonSummary> findAllProjectedNativeQuery();
PersonSummary findProjectedById(long id);
@Query(value = "SELECT p.name AS name, p.age AS age FROM Person AS p WHERE id = ?1")
PersonSummary findProjectedByIdJpaQuery(long id);
@Query(value = "SELECT p.name AS name, p.age AS age FROM Person AS p", nativeQuery = true)
PersonSummary findProjectedByIdNativeQuery(long id);
}
您可以找到具体示例here。