简体   繁体   English

Output Java Spring 引导 Z9CE3D1BD8890F16A087C44809359Z 中的重复项

[英]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: MotionPicture 的 RestController:

@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表:剪辑列:id(1-100 唯一)、日期、大小

Table: motion_picture columns: id(1-100 unique), date, size, duration表:motion_picture列:id(1-100 唯一)、日期、大小、持续时间

association table: motion_picture_clips columns: motion_picture_id (1-100 random), clips_id (1-100 unique)关联表:motion_picture_clips列:motion_picture_id(1-100 随机),clips_id(1-100 唯一)

When I run the programm and make a the getRequest in Postman (getById and getAll) --> I'll get duplicated values back当我运行程序并在 Postman (getById 和 getAll)中创建 getRequest 时——>我会得到重复的值

JSON from Postman: ( localhost/DB/motionpicture/2 ) JSON 来自 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?如何更改代码或数据库以仅获取一个 id 为 73 的剪辑?

I would be very grateful for your help:)我将非常感谢您的帮助:)

Kind regards!亲切的问候!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM