mysql - CrudRepository: findAll() stucks in infinite loop -
i have onetomany
data model 2 entities. 1 machine contains many characteristics.
problem: when try complete data of data base program stucks in infinite loop. see json result - data repeated time...
[{"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":
and on...
i not know why happens. inserting data programatically seems work fine! current data produced following lines of code:
@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 = 0; < 6; i++){ machine.addcharacteristic(new characteristic("character" + integer.tostring(i),0,0)); } machinerepository.save(machine); return (collection<machine>) machinerepository.findall(); }
question: infinite loop coming from?
the database model
machine:
create statement:
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:
@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; } }
characteristic:
create statement:
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:
@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>{}
in code return collection of machine
. class has one-to-many dependency characteristic
. characteristic
class has many-to-one dependency om machine
.
when machine
object serialized it's looking on dependency characteristic
last looking on machyne
, , on. these cycle dependencies impossible serialize until exclude properties reference same object. should exclude either one-to-many or many-to-one properties.
Comments
Post a Comment