【问题标题】:Duplicates in Output Java Spring Boot JPA输出 Java Spring Boot JPA 中的重复项
【发布时间】:2021-01-06 15:36:59
【问题描述】:

我正在开发具有以下实体和代码的应用程序:

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

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

我的数据库

表格:剪辑 列:id(1-100 唯一)、日期、大小

表格:motion_picture 列:id(1-100 唯一)、日期、大小、持续时间

关联表:motion_picture_clips 列:motion_picture_id(1-100 随机),clips_id(1-100 唯一)

当我运行程序并在 Postman 中创建 getRequest(getById 和 getAll)--> 我会得到重复的值

来自 Postman 的 JSON:(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
        }
    ]
}

如何更改代码或数据库以仅获取一个 id 为 73 的剪辑?

非常感谢您的帮助:)

亲切的问候!

【问题讨论】:

    标签: java mysql spring-boot hibernate spring-data-jpa


    【解决方案1】:

    尝试使用

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

    而不是:

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

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 2015-12-28
      • 1970-01-01
      • 2016-09-13
      • 2018-02-20
      • 2020-03-09
      • 2017-10-28
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多