Skip to content
Advertisement

Duplicates in Output Java Spring Boot JPA

I’m working on an application with following Entities and code:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Clip {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private long size;
    private long date;


    public Clip() {
    }
    
    public long getId() {
        return id;
    }

    public long getSize() {
        return size;
    }

    public long getDate() {
        return date;
    }
}`

@Entity
public class MotionPicture extends Clip {

    private long duration;

    @OneToMany(fetch = FetchType.LAZY)
    List<Clip> clips = new ArrayList<Clip>();


    public MotionPicture() {
    }

    public MotionPicture(long duration) {
        this.duration = duration;
    }

    public List<Clip> getClips() {
        return clips;
    }


    public long getDuration() {
        return duration;
    }
}

The RestController for MotionPicture:

@CrossOrigin
public class MotionPictureRestController {

    @Autowired
    private MotionPictureRepository motionPictureRepository;

    @GetMapping(value = "universal2/motionpicture")
    public ResponseEntity<List<MotionPicture>> getMotionPicture() {
        List<MotionPicture> result = this.motionPictureRepository.findAll();

        if (!result.isEmpty()) {
            return new ResponseEntity<List<MotionPicture>>(result, HttpStatus.OK);
        } else {
            return new ResponseEntity<List<MotionPicture>>(HttpStatus.NOT_FOUND);
        }
    }


    @GetMapping(value = "universal2/motionpicture/{id}")
    public ResponseEntity<MotionPicture> getMotionPictureById(@PathVariable("id") long id) {
        Optional<MotionPicture> result = this.motionPictureRepository.findById(id);

        if (result.isPresent()) {
            return new ResponseEntity<MotionPicture>(result.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity<MotionPicture>(HttpStatus.NOT_FOUND);
        }
    }
}

My DB

Table: clip columns: id(1-100 unique), date, size

Table: motion_picture columns: id(1-100 unique), date, size, duration

association table: motion_picture_clips columns: motion_picture_id (1-100 random), clips_id (1-100 unique)

When I run the programm and make a the getRequest in Postman (getById and getAll) –> I’ll get duplicated values back

JSON from Postman: (localhost/DB/motionpicture/2)

{
    "id": 2,
    "size": 7040,
    "date": 2006,
    "duration": 2899,
    "clips": [
        {
            "id": 73,
            "size": 7246,
            "date": 2009
        },
        {
            "id": 73,
            "size": 7246,
            "date": 2009
        }
    ]
}

How can I change the code or the DB to get only one clip with id 73 back?

I would be very grateful for your help:)

Kind regards!

Advertisement

Answer

Try to use

@OneToMany(fetch = FetchType.LAZY)
Set<Clip> clips = new HashSet<Clip>();

instead of :

@OneToMany(fetch = FetchType.LAZY)
List<Clip> clips = new ArrayList<Clip>();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement