java - Spring MVC JPA @ManyToMany Bidirectional -


hope things going well.

i attempting implement @manytomany bidirectional mapping in spring mvc utilizing spring boot, spring data jpa, hibernate, gradle, , spring data rest. when implement mapping between 2 (2) entities, events , tags. event can 0 many tags , tag can associated 0 many events. in fact able save pojos hibernate session, including hashset<>() through setter of mapped entities. however, as attempt query list of pojos through crudrepository method such .findall(), blows. looking @ console output, looks hibernate attempting append other object hundred+ times. i'm using @restcontroller the list action (.findall()), when navigate restful endpoint, instead of showing 2 json objects, shows massive unclosed appended mess of json fragments. console output indicating jackson databind may having issues serializing .findall() results hibernate outputting.

any appreciated. biggest confusion me why hibernate attempt append result string so many times. perhaps issue with:

  1. @manytomany mapping configuration? needs @joincolumns on tag entity?
  2. fetch = fetchtype.eager?
  3. lacking/excessive getter/setter? remove/add getter/setter either event or tag entities?
  4. spring boot and/or gradle? other example can find on interwebs utilizing maven.
  5. dependencies? spring-starter-boot-starter-data-jpa vs spring-data-jpa?
  6. constructors? (event.java not have parameter constructor tags)
  7. hashset<>() property type events and/or tags?

build.gradle

dependencies {     compile("org.springframework.boot:spring-boot-starter-data-rest")     compile("org.springframework.boot:spring-boot-starter-web")     compile("org.springframework.boot:spring-boot-starter-data-jpa")     compile("org.hibernate:hibernate-validator")     compile("org.apache.tomcat.embed:tomcat-embed-el")     compile("com.h2database:h2") } 

event.java

package event.model;  import org.hibernate.validator.constraints.email; import org.springframework.format.annotation.datetimeformat;  import javax.persistence.*; import javax.validation.constraints.future; import javax.validation.constraints.notnull; import javax.validation.constraints.size; import java.util.*;  @entity @table(uniqueconstraints = {@uniqueconstraint(columnnames = {"title", "date"})}) public class event {      @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @notnull     @size(min = 2, max = 55)     private string title;      // .. additional private variables      @manytomany(fetch = fetchtype.eager)     @jointable(             name = "events_tags",             joincolumns=@joincolumn(name = "event_id"),             inversejoincolumns = @joincolumn(name="tag_id")     )     private set<tag> tags = new hashset<>();      public event() {     }      public event(string title, string description, date date, string organizer, string email, string location) {         this.title = title;         this.description = description;         this.date = date;         this.organizer = organizer;         this.email = email;         this.location = location;     }      public long getid() {         return id;     }      // .. getters , setters other properties      public set<tag> gettags(){         return tags;     }      public void settags(set<tag> tags){         this.tags = tags;     }      // @overide tostring() method } 

tag.java

package event.model;  import javax.persistence.*; import javax.validation.constraints.notnull; import javax.validation.constraints.size; import java.util.*;  @entity public class tag {      @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @notnull     @size(min = 3, max = 255)     private string title;      @manytomany(mappedby = "tags", fetch = fetchtype.eager)     private set<event> events = new hashset<>();      public tag() {     }      public tag(string title) {         this.title = title;     }      public long getid() {         return id;     }      // .. getters , setters      public set<event> getevents() {         return events;     }      public void setevents(set<event> events){         this.events = events;     }      // @override tostring() method 

}

application.java

@springbootapplication public class application implements commandlinerunner {      @autowired     eventrepository eventrepository;      @autowired     tagrepository tagrepository;      public static void main(string[] args) throws exception {         springapplication.run(application.class, args);     }      @override     public void run(string... strings) throws exception {         simpledateformat formatter = new simpledateformat("mm-dd-yyyy hh:mm a");         string[] datestrings = {"07-25-2015 12:00 pm", "08-25-2015 12:00 pm"};          date date1 = formatter.parse(datestrings[0]);         date date2 = formatter.parse(datestrings[1]);          event event1 = eventrepository.save(new event("bacon day", "this day bacon. need bacon time", date1, "john smith", "john.smith@test.com", "w523"));         event event2 = eventrepository.save(new event("salad day", "this day salad. need salad time", date2, "jane smith", "jane.smith@test.com", "n423"));          tag tag1 = tagrepository.save(new tag("savory"));         tag tag2 = tagrepository.save(new tag("vegetarian"));          event1.gettags().add(tag1);         event1.gettags().add(tag2);         event1 = eventrepository.save(event1);         system.out.println(event1);         system.out.println(eventrepository.findall());     } } 

console error output

hibernate: select events0_.tag_id tag_id2_2_0_, events0_.event_id event_id1_1_0_, event1_.id id1_0_1_, event1_.date date2_0_1_, event1_.description descript3_0_1_, event1_.email email4_0_1_, event1_.location location5_0_1_, event1_.organizer organize6_0_1_, event1_.title title7_0_1_ events_tags events0_ inner join event event1_ on events0_.event_id=event1_.id events0_.tag_id=? 2015-07-13 18:35:28.084 error 4088 --- [nio-8080-exec-1] o.a.c.c.c.[.[.[/].[dispatcherservlet]    : servlet.service() servlet [dispatcherservlet] in context path [] threw exception [handler processing failed; nested exception java.lang.stackoverflowerror] root cause  java.lang.stackoverflowerror: null     @ java.util.date.getyear(date.java:651)     @ java.sql.timestamp.tostring(timestamp.java:279)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ event.model.event.tostring(event.java:128)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ java.util.abstractcollection.tostring(abstractcollection.java:462)     @ org.hibernate.collection.internal.persistentset.tostring(persistentset.java:317)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ event.model.tag.tostring(tag.java:51)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ java.util.abstractcollection.tostring(abstractcollection.java:462)     @ org.hibernate.collection.internal.persistentset.tostring(persistentset.java:317)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ event.model.event.tostring(event.java:128)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ java.util.abstractcollection.tostring(abstractcollection.java:462)     @ org.hibernate.collection.internal.persistentset.tostring(persistentset.java:317)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ event.model.tag.tostring(tag.java:51)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131)     @ java.util.abstractcollection.tostring(abstractcollection.java:462)     @ org.hibernate.collection.internal.persistentset.tostring(persistentset.java:317)     @ java.lang.string.valueof(string.java:2982)     @ java.lang.stringbuilder.append(stringbuilder.java:131) 

that continues hundreds more lines.

try using @jsonignore in child class on parents collection. trying serialize recursively.

follow link other ways handle this.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -