iammyr
iammyr

Reputation: 281

Spring Data JPA to handle a complex type

I'd immensely appreciate if anyone could help with the following issue.

My RESTful web service exposes a Scan resource with no issue. However, when I try to add the attribute "attr1" as @ManyToMany to Scan of type Collection

@ManyToMany
Collection<AnotherType> attr1;

I get the following error (after running mvn spring-boot:run):

org.hibernate.MappingException: Could not determine type for: whatever.AnotherTypeSubOne, at table: anothertype, for columns: [org.hibernate.mapping.Column(another_type_sub_one)]

where AnotherType is an @Entity and has 3 attributes of the following types:

  1. AnotherTypeSubOne,
  2. AnotherTypeSubTwo,
  3. Collection as @ManyToMany(mappedBy = "attr1")

AnotherTypeSubOne and AnotherTypeSubTwo are also @Entity and they only contain attributes of type String. The thirs attribute is:

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

What am I doing wrong? Will I manage to get Spring to automatically handle a JSON representation of the complex type Collection?

Thank you so much!

What I want is to have GET /scans/ returning a JSON representation of the scan entity that includes that complex attribute "attr1".

For whomever prefers to go through the original code itself, here it is.

@Entity
public class Scan {

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


    private Long projectId;

    @ManyToMany
    private Collection<Result> result;

<getter/setter methods>

Class Result

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

Class Second

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

That is all the code (it's relying on H2 embedded database). In the 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>

Upvotes: 0

Views: 2647

Answers (1)

JB Nizet
JB Nizet

Reputation: 692073

private First vulnerability;

private Second pathNode;

should be

@ManyToOne
private First vulnerability;

@ManyToOne
private Second pathNode;

(or @OneToOne if the vulnerability/pathNode belongs to only one Result).

Upvotes: 1

Related Questions