我有一个带有两个实体的 OneToMany 数据模型。一台机器包含许多特性。

问题: 当我尝试获取数据库的完整数据时,程序陷入无限循环。查看 JSON 结果 - 数据一直重复......

[{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":[{"name":null,"description":"CHARACTER2","type":0,"value":0,"machine":{"name":"Neue Machine","description":"Description der neuen machine","characteristics":

等等...

我真的不知道为什么会这样。以编程方式插入数据似乎 工作正常 !
当前数据由以下代码行生成:
@RequestMapping(value = "/machine", method = RequestMethod.GET)
    Collection<Machine> readMachines(){

        Machine machine = new Machine("Neue Machine", "Description der neuen machine");
        //Set<Characteristic> newCharacter =  new HashSet<Characteristic>();
        for(int i = 0; i < 6; i++){
            machine.addCharacteristic(new Characteristic("CHARACTER" + Integer.toString(i),0,0));
        }

        machineRepository.save(machine);

        return (Collection<Machine>) machineRepository.findAll();
    }

问题: 无限循环从何而来?

数据库模型

机器:

mysql - CrudRepository : findAll() stucks in infinite loop-LMLPHP

创建语句:
CREATE TABLE `machine` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `description` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8

实体:
@Entity
@Table(name = "characteristic")
public class Characteristic {
    private int characteristic_id;

    private String name;

    private String description;

    private int type;

    private int value;

    private Machine machine;

    @ManyToOne
    @JoinColumn(name="machine_id")
    public Machine getMachine(){
        return machine;
    }

    public void setMachine(Machine machine){
        this.machine = machine;
    }

    public Characteristic() {}

    public Characteristic(String description, int type, int value) {
        this.description = description;
        this.type = type;
        this.value = value;
    }

    public Characteristic(int characteristic_id, String description, int type, int value) {
        this.characteristic_id = characteristic_id;
        this.description = description;
        this.type = type;
        this.value = value;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="characteristic_id")
    public int getCharacteristic_Id() {
        return characteristic_id;
    }

    public void setCharacteristic_Id(int characteristic_id) {
        this.characteristic_id = characteristic_id;
    }

    @Column(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name="type")
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Column(name="value")
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

特性:

mysql - CrudRepository : findAll() stucks in infinite loop-LMLPHP

创建语句:
CREATE TABLE `characteristic` (
  `characteristic_id` int(11) NOT NULL AUTO_INCREMENT,
  `machine_id` int(11) NOT NULL DEFAULT '0',
  `name` varchar(45) DEFAULT NULL,
  `description` varchar(45) DEFAULT NULL,
  `type` int(11) NOT NULL,
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`characteristic_id`),
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1

实体:
@Entity
@Table(name = "machine")
public class Machine {
    private int machine_id;

    private String name;

    private String description;

    private Set<Characteristic> characteristics;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "machine", cascade = CascadeType.ALL)
    public Set<Characteristic> getCharacteristics() {
        return characteristics;
    }

    public void setCharacteristics(Set<Characteristic> characteristics){
        this.characteristics = characteristics;
    }

    public Machine(){}

    public Machine(String name, String description){
        this.name = name;
        this.description = description;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    public int getId() {
        return machine_id;
    }

    public void setId(int machine_id) {
        this.machine_id = machine_id;
    }

    @Column(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void addCharacteristic(Characteristic characteristic){
        if(this.characteristics == null){
            this.characteristics = new HashSet<Characteristic>();
        }
        characteristic.setMachine(this);
        this.characteristics.add(characteristic);
    }
}

CrudRepository:
public interface MachineRepository extends CrudRepository<Machine, Integer>{}

最佳答案

在您的代码中,您返回 Machine 的集合。此类对 Characteristic 具有一对多依赖关系。 Characteristic 类具有多对一的依赖关系 om Machine

Machine 对象被序列化时,它查看依赖项 Characteristic ,最后查看 Machyne ,依此类推。这些循环依赖项不可能序列化,除非您排除引用同一对象的属性。您应该排除一对多或多对一属性。

关于mysql - CrudRepository : findAll() stucks in infinite loop,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38072574/

10-12 03:43