[英]Spring Data JPA to handle a complex type
如果有人可以帮助解决以下问题,我将深表感谢。
我的RESTful Web服务公开了一个扫描资源,没有任何问题。 但是,当我尝试将属性“ attr1”作为@ManyToMany添加到集合类型的扫描中时
@ManyToMany
Collection<AnotherType> attr1;
我收到以下错误(在运行mvn spring-boot:run之后):
org.hibernate.MappingException:无法确定类型:what.AnotherTypeSubOne,在表:othertype,用于列:[org.hibernate.mapping.Column(another_type_sub_one)]
其中AnotherType是@Entity,并具有以下类型的3个属性:
AnotherTypeSubOne和AnotherTypeSubTwo也是@Entity,它们仅包含String类型的属性。 thirs属性是:
@ManyToMany(mappedBy = "attr1")
Collection<Scan> scan;
我究竟做错了什么? 我是否可以让Spring自动处理复杂类型Collection的JSON表示形式?
非常感谢!
我想要的是让GET / scans /返回包含复杂属性“ attr1”的扫描实体的JSON表示形式。
对于喜欢通过原始代码本身的人来说,就在这里。
@Entity
public class Scan {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long projectId;
@ManyToMany
private Collection<Result> result;
<getter/setter methods>
班级成绩
@Entity
public class Result {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private First vulnerability;
private Second pathNode;
@ManyToMany(mappedBy = "result")
private Collection<Scan> scan;
<getter/setter methods>
类第一
@Entity
public class First {
@Id
private String id;
private String name;
private long score;
private String description;
第二课
@Entity
public class Second {
@Id
private Long id;
private String name;
private int line;
private int col;
private String snippet;
类ScanRepository
@RepositoryRestResource(collectionResourceRel = "scans", path = "scans")
public interface ScanRepository extends PagingAndSortingRepository<Scan, Long> {
/**
* Custom query to retrieve a list of Scan objects based on their project's
* ID.
*
* @param pid project id
* @return
*/
List<Scan> findByProjectId(@Param("pid") String pid);
这就是全部代码(它依赖于H2嵌入式数据库)。 在pom.xml中:
<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>com.waratek</groupId>
<artifactId>waratek-rasp</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>waratek-rasp</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- Necessary in order to get rid of
java.lang.ClassFormatError: Absent Code attribute in method that is not
native or abstract in class file javax/faces/webapp/FacesServlet-->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<repositories>
<!-- <repository>
<id>Java.Net</id>
<url>http://download.java.net/maven/2/</url>
</repository>-->
<!-- <repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>-->
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
private First vulnerability;
private Second pathNode;
应该
@ManyToOne
private First vulnerability;
@ManyToOne
private Second pathNode;
(如果漏洞/路径节点仅属于一个结果,则为@OneToOne
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.