Nested data structure with Spring Data JDBC

北战南征 提交于 2019-12-23 12:16:32

问题


I wanted to create a nested datastructure.

Entity1 contains Objects of type Entity2 stored in a Map. Entity2 should contain a Map of Objects of Entity3.

The first part, Entity1 and Entity works fine. When I add Entity3, an Exception occurs.

When I executed a simple test the following Exception occured:

java.lang.IllegalArgumentException: Target bean of type org.springframework.data.util.Pair is not of type of the persistent entity (org.hameister.filmwatcher.nested.Entity2)!:org.springframework.data.util.Pair

    package org.hameister.nested;

    import org.springframework.data.annotation.Id;

    import java.util.HashMap;
    import java.util.Map;

    public class Entity1 {
    @Id
    long id;

    String name1;

    Map<String, Entity2> bMap = new HashMap<>();

    public Entity1() {
    }

    public Entity1(String name1) {
        this.name1 = name1;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public  void  addElement(Entity2 entity2) {
        bMap.put(entity2.name2, entity2);
    }

 }

Entity2:

package org.hameister.nested;

import org.springframework.data.annotation.Id;

import java.util.HashMap;
import java.util.Map;

public class Entity2 {
    @Id
    long id;
    String name2;

//    Map<String,Entity3> map = new HashMap<>();

    public Entity2() {
    }

    public Entity2(String name2) {
        this.name2 = name2;
    }

//    public void  addElement(Entity3 entity3) {
//       map.put(entity3.name3, entity3);
//    }

    public void setId(Long id) {
        this.id = id;
    }
}

Entity3:

package org.hameister.nested;

import org.springframework.data.annotation.Id;

public class Entity3 {

    @Id
    long id;
    String name3;

    public Entity3(String name3) {
        this.name3 = name3;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

When I added another class Entity3 which should be stored in Entity2 in a Map, this didn't not work. When I executed a simple test the following Exception occurred:

java.lang.IllegalArgumentException: Target bean of type org.springframework.data.util.Pair is not of type of the persistent entity (org.hameister.filmwatcher.nested.Entity2)!: org.springframework.data.util.Pair

To reproduce the Exception simple remove the double slashes in the class Entity2. And the double slash in the test NestedTest.

Here is the test:

package org.hameister.nested;

import org.hameister.nested.Entity1;
import org.hameister.nested.Entity2;
import org.hameister.nested.Entity3;
import org.hameister.nested.Repository1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJdbcTest
public class NestedTest {

    @Autowired
    Repository1 repository1;

    @Test
    public void testA() {

        Entity1 entity1 = new Entity1("Entity1");
        Entity2 entity2 = new Entity2("Entity2");
        Entity3 entity3 = new Entity3("Entity3");
        entity1.addElement(entity2);
     //   entity2.addElement(entity3);
        repository1.save(entity1);


    }
}

The complete source code can be found here.

来源:https://stackoverflow.com/questions/53176891/nested-data-structure-with-spring-data-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!