Spring Data JPA处理复杂类型

时间:2015-05-26 16:46:13

标签: java spring hibernate spring-mvc jpa

如果有人能帮助解决以下问题,我非常感激。

我的RESTful Web服务公开了一个没有问题的扫描资源。但是,当我尝试添加属性" attr1"作为@ManyToMany扫描类型集合

@ManyToMany
Collection<AnotherType> attr1;

我收到以下错误(运行mvn spring-boot:run之后):

  

org.hibernate.MappingException:无法确定:whatever.AnotherTypeSubOne的类型,在表:anothertype,对于列:[org.hibernate.mapping.Column(another_type_sub_one)]

其中AnotherType是@Entity并具有以下类型的3个属性:

  1. AnotherTypeSubOne,
  2. AnotherTypeSubTwo,
  3. 收藏为@ManyToMany(mappedBy =&#34; attr1&#34;)
  4. AnotherTypeSubOne和AnotherTypeSubTwo也是@Entity,它们只包含String类型的属性。 thirs属性是:

    @ManyToMany(mappedBy = "attr1")
    Collection<Scan> scan;
    

    我做错了什么?我是否设法让Spring自动处理复杂类型Collection的JSON表示?

    非常感谢你!

    我想要的是让GET /扫描/返回包含该复杂属性的扫描实体的JSON表示&#34; attr1&#34;。

    对于喜欢通过原始代码本身的人来说,就是这样。

    @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>
    

    Class First

    @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;
    

    Class 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>
    

1 个答案:

答案 0 :(得分:1)

private First vulnerability;

private Second pathNode;

应该是

@ManyToOne
private First vulnerability;

@ManyToOne
private Second pathNode;

(如果漏洞/路径节点只属于一个结果,则为@OneToOne