4

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.

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
hameiste
  • 41
  • 5
  • Sounds like a bug. Please try with the latest 1.1.0-SNAPSHOT and if it exists there as well create an issue at https://jira.spring.io/projects/DATAJDBC – Jens Schauder Nov 07 '18 at 05:52
  • Ok, I will do that. – hameiste Nov 07 '18 at 06:15
  • The problem occurs with the latest version1.1.0 288 SNAPSHOT, too. I will create an issue... – hameiste Nov 07 '18 at 10:53
  • Issue for the bug: https://jira.spring.io/browse/DATAJDBC-291 – hameiste Nov 07 '18 at 11:41
  • 1
    It's just awful ): – Pospolita Nikita Dec 11 '18 at 14:37
  • @JensSchauder was there ever a fix for this? I am having the same issue – Daniel Haughton May 22 '19 at 14:09
  • @DanielHaughton DATAJDBC-291 was probably a duplicate of https://jira.spring.io/browse/DATAJDBC-223 for which a fix was just merged to master today. If you or anybody could give the latest 1.1 Snapshot a try and tell me the results that would be great. https://repo.spring.io/libs-snapshot-local/org/springframework/data/spring-data-jdbc/1.1.0.BUILD-SNAPSHOT/ – Jens Schauder May 22 '19 at 14:25
  • Thanks jen. Just to be clear this is in a JHipster generated spring app.I am getting JPA from spring-boot-starter-data-jpa. Like the following in the pom org.springframework.boot spring-boot-starter-data-jpa ${spring-boot.version} – Daniel Haughton May 22 '19 at 14:31
  • Can I change it to use the new jpa released today? – Daniel Haughton May 22 '19 at 14:32
  • 1
    @DanielHaughton This question has nothing to do with spring-data-jpa. This is about spring-data-jdbc. – Jens Schauder May 22 '19 at 14:47
  • @JensSchauder my mistake that is what I meant. So in my pom is org.springframework.boot spring-boot-starter-data-jdbc – Daniel Haughton May 22 '19 at 14:52
  • @DanielHaughton start by configuring the latest milestone of spring boot and then the mentioned version of spring-data-jdbc. You might have to add some bean configurations. If you have problems with that please create a new question. – Jens Schauder May 23 '19 at 07:00

1 Answers1

0

To collect the information from comments and the issue tracker:

This was a bug back in the day but is fixed since version 1.1.4.

I anyone is still seeing this with a current version please create a ticket.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348