为什么我从findById()方法Spring Data中获得一个乘法数据?

时间:2019-08-17 15:37:59

标签: spring spring-data lombok

大家好,我最近与Spring Data合作,我有一个问题。

我的实体映射如下:

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Table(name = "movie")
@Entity
public class Movie extends BaseEntity {

    private String name;
    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "enum('ACTION','DRAMA','COMEDY','FANTASY','HORROR')")
    private Genre genre;

    private BigDecimal budget;

    @ManyToMany(mappedBy = "movies")
    private Set<Actor> actors;

    public Movie() {
    }
}

我的演员实体:

@Getter
@Setter
@EqualsAndHashCode(callSuper = true, exclude = "movies")
@ToString(callSuper = true, exclude = "movies")
@Table(name = "actor")
@Entity
public class Actor extends BaseEntity {

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "salary")
    private BigDecimal salary;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "actor_movie",
            joinColumns = {@JoinColumn(name = "actor_id")},
            inverseJoinColumns = {@JoinColumn(name = "movie_id")}
    )
        private Set<Movie> movies;

    public Actor() {
    }
}

还有我的BaseEntity;

@Data
@MappedSuperclass
public class BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

我正在使用JpaRepository<Actor, Long>从数据库中获取演员:

@Repository
public interface ActorRepository extends JpaRepository<Actor, Long> {
}

这是我的控制器:

@RestController
@RequestMapping("actor")
public class ActorController {

    @Autowired
    ActorRepository actorRepository;

    @PostMapping
    public Actor save(@RequestBody Actor actor) {
        actorRepository.save(actor);
        return actor;
    }

    @GetMapping
    public Iterable<Actor> findAllActors() {
        return actorRepository.findAll();
    }

    @GetMapping("{id}")
    public Actor findActor(@PathVariable Long id) {
        return actorRepository.findById(id).orElseThrow(NotFoundException::new);
    }

    @PutMapping("{id}")
    public Actor update(@RequestBody Actor actor, @PathVariable Long id) {
        actor.setId(id);
        return actorRepository.save(actor);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Actor> delete(@PathVariable Long id) {
        actorRepository.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}

因此,当我尝试通过id获取演员时,它得到了很多次: enter image description here

最后它失败并出现StackOverFlowError:

java.lang.StackOverflowError: null
    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_172]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_172]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_172]
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_172]
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_172]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_172]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_172]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_172]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_172]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_172]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_172]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_172]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:737) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.9.jar:2.9.9]

为什么会这样?以及如何解决?

0 个答案:

没有答案