JPA @ManyToMany Java.lang.StackOverflowError:null

Source: Internet
Author: User

JPA lazy loaded a pit great God can detour

Package Com.liuzm.controller;

Import Com.liuzm.entity.UacRole;
Import Com.liuzm.entity.UacUser;
Import Com.liuzm.service.UacUserService;
Import org.springframework.web.bind.annotation.GetMapping;
Import Org.springframework.web.bind.annotation.RestController;

Import Javax.annotation.Resource;
Import Java.util.Set;

@RestController public
class Usercontroller {

    @Resource
    private uacuserservice uacuserservice;

    @GetMapping ("/") Public
    set<uacrole> Findbyloginname () {
        Uacuser uacuser = This.uacUserService.findByLoginName ("admin");
        System.out.println (uacuser);
        set<uacrole> roles = Uacuser.getroles ();
        System.out.println (Roles.size ());
        return roles
    }
}
Package com.liuzm.entity;

Import Com.fasterxml.jackson.annotation.JsonIgnore;
Import javax.persistence.*;
Import java.io.Serializable;
Import Java.util.HashSet;

Import Java.util.Set; @Entity @Table (name = "Tbl_uac_role") public class Uacrole implements Serializable {/** * Serialversionuid: In a sentence
     This variable represents what.

    * @since JDK 1.7 * * Private static final long serialversionuid = -6263200210499558891l;

    /** * ID */@Id @GeneratedValue (strategy = generationtype.identity) Private Long ID;
    /** * Role Code */@Column (name = "Role_code") private String Rolecode; @JsonIgnore @ManyToMany @JoinTable (name = "Tbl_uac_role_user", Joincolumns = {@JoinColumn (name = "role_id"), Refere Ncedcolumnname = "id"}, Inversejoincolumns = {@JoinColumn (name = "User_ID", referencedcolumnname = "id")}) Private Se

    t<uacuser> users = new hashset<> (); Public Uacrole () {} public uacrole (Long id) {this.id = ID;
    Public Long GetId () {return id;
    public void SetId (Long id) {this.id = ID;
    Public String Getrolecode () {return rolecode;
    } public void Setrolecode (String rolecode) {this.rolecode = Rolecode;
    Public set<uacuser> Getusers () {return users;
    public void Setusers (set<uacuser> users) {this.users = users; @Override public String toString () {return "uacrole{" + "id=" + ID + "
    , rolecode= ' + rolecode + ' \ ' + ', users= ' + Users + '} '; }
}
Package com.liuzm.entity;

Import Com.fasterxml.jackson.annotation.JsonIgnore;
Import javax.persistence.*;
Import java.io.Serializable;
Import Java.util.HashSet;

Import Java.util.Set; @Entity @Table (name = "Tbl_uac_user") public class Uacuser implements Serializable {/** * Serialversionuid: In a sentence
     This variable represents what.

    * @since JDK 1.7/private static final long serialversionuid = 3439493474130693948L;

    /** * ID */@Id @GeneratedValue (strategy = generationtype.identity) Private Long ID;
    /** * Login name/@Column (name = "login_name") private String loginName;

    @ManyToMany (Mappedby = "users") @JsonIgnore private set<uacrole> roles = new hashset<> ();
                @Override public String toString () {return "uacuser{" + "id= '" + ID + ' \ "+
    ", loginname= '" + loginName + ' + ", roles=" + Roles + '} ";
   Public Uacuser () {} Public Uacuser (Long id) {this.id = ID;
    Public Long GetId () {return id;
    public void SetId (Long id) {this.id = ID;
    Public String Getloginname () {return loginName;
    } public void Setloginname (String loginName) {this.loginname = LoginName;
    Public set<uacrole> GetRoles () {return roles;
    public void Setroles (set<uacrole> roles) {this.roles = roles; }
}
Package com.liuzm.repository;

Import Com.liuzm.entity.UacUser;
Import org.springframework.data.jpa.repository.JpaRepository;


Public interface Uacuserrepository extends Jparepository<uacuser, long> {

    uacuser findbyloginname (String loginName);
}
Package com.liuzm.service;

Import Com.liuzm.entity.UacUser;

Public interface Uacuserservice {

    uacuser findbyloginname (String loginName);
}
Server:
  port:8000
  Tomcat:
    uri-encoding:utf-8
Spring:
  application:
    name:jpa-test
  JPA:
      generate-ddl:false
      show-sql:true
      hibernate:
        ddl-auto:update
      Properties:
        Hibernate:
          dialect:org.hibernate.dialect.MySQL5Dialect
  DataSource:
    driver-class-name: Com.mysql.jdbc.Driver
    Url:jdbc:mysql://localhost/test?useunicode=true&characterencoding=utf-8
    Username:root
    password:123456
logging: Level
  :
    root:info
    Org.hibernate:INFO
    Org.hibernate.type.descriptor.sql.BasicBinder:TRACE
    Org.hibernate.type.descriptor.sql.BasicExtractor: TRACE

Errors are as follows

Hibernate:select uacuser0_.id as Id1_2_, uacuser0_.login_name as login_na2_2_ from Tbl_uac_user uacuser0_ where uacuser0_
. login_name=?  2017-04-19 13:49:46.187 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicbinder:binding parameter [1]   As [VARCHAR]-[admin] 2017-04-19 13:49:46.191 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextractor : Extracted value ([id1_2_]: [BIGINT])-[222] 2017-04-19 13:49:46.195 TRACE 14504---[nio-8000-exec-1] O.h.type.descri ptor.sql.BasicExtractor:extracted value ([login_na2_2_]: [VARCHAR])-[admin] hibernate:select roles0_.user_id as US er_id2_1_0_, roles0_.role_id as role_id1_1_0_, uacrole1_.id as id1_0_1_, Uacrole1_.role_code as role_cod2_0_1_ from Tbl_u
Ac_role_user roles0_ INNER JOIN Tbl_uac_role uacrole1_ on roles0_.role_id=uacrole1_.id where roles0_.user_id=?  2017-04-19 13:49:46.208 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicbinder:binding parameter [1] As [BIGINT]-[222] 2017-04-19 13:49:46.213 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextractor:extracted value ([id1_ 0_1_]: [BIGINT])-[1234] 2017-04-19 13:49:46.213 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextract or:extracted value ([role_cod2_0_1_]: [VARCHAR])-[admin] 2017-04-19 13:49:46.214 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextractor:extracted value ([user_id2_1_0_]: [BIGINT])-[222] 2017-04-19 13:49:46.214 TR ACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextractor:extracted value ([role_id1_1_0_]: [BIGINT])- [1234] Hibernate:select users0_.role_id as role_id1_1_0_, users0_.user_id as user_id2_1_0_, uacuser1_.id as id1_2_1_, UAC User1_.login_name as login_na2_2_1_ from Tbl_uac_role_user users0_ inner join Tbl_uac_user uacuser1_ on Users0_.user_id=u
Acuser1_.id where users0_.role_id=? 2017-04-19 13:49:46.217 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicbinder:binding parAmeter [1] as [BIGINT]-[1234] 2017-04-19 13:49:46.218 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicext ractor:extracted value ([id1_2_1_]: [BIGINT])-[222] 2017-04-19 13:49:46.218 TRACE 14504---[nio-8000-exec-1] o.h.t ype.descriptor.sql.BasicExtractor:extracted value ([role_id1_1_0_]: [BIGINT])-[1234] 2017-04-19 13:49:46.218 TRACE 14504---[nio-8000-exec-1] o.h.type.descriptor.sql.basicextractor:extracted value ([user_id2_1_0_]: [BIGINT])-[222 ] 2017-04-19 13:49:46.271 ERROR 14504---[nio-8000-exec-1] o.a.c.c.c.[.[. [/]. [Dispatcherservlet]: Servlet.service () for Servlet [Dispatcherservlet] in context with path [] threw exception [Handle R Dispatch failed; Nested exception is java.lang.StackOverflowError] with root cause java.lang.StackOverflowError:null at Java.util.Abs Tractcollection.tostring (abstractcollection.java:454) ~[na:1.8.0_60] at Org.hibernate.collection.internal.PersistentSet.toString (persistentset.java:300) ~[hibernate-coRe-5.0.12.final.jar:5.0.12.final] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at java.lang.String Builder.append (stringbuilder.java:131) ~[na:1.8.0_60] at com.liuzm.entity.UacUser.toString (uacuser.java:37) ~[ Classes/:na] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at Java.lang.StringBuilder.append (String builder.java:131) ~[na:1.8.0_60] at java.util.AbstractCollection.toString (abstractcollection.java:462) ~[na:1.8.0_ At Org.hibernate.collection.internal.PersistentSet.toString (persistentset.java:300) ~[ Hibernate-core-5.0.12.final.jar:5.0.12.final] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at Java . lang. Stringbuilder.append (stringbuilder.java:131) ~[na:1.8.0_60] at com.liuzm.entity.UacRole.toString (uacrole.java:69) ~[classes/:na] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at Java.lang.StringBuilder.append (stri ngbuilder.java:131) ~[na:1.8.0_60] at Java.util.AbstractCoLlection.tostring (abstractcollection.java:462) ~[na:1.8.0_60] at Org.hibernate.collection.internal.PersistentSet.toString (persistentset.java:300) ~[ Hibernate-core-5.0.12.final.jar:5.0.12.final] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at Java . lang. Stringbuilder.append (stringbuilder.java:131) ~[na:1.8.0_60] at com.liuzm.entity.UacUser.toString (uacuser.java:37) ~[classes/:na] at java.lang.String.valueOf (string.java:2994) ~[na:1.8.0_60] at Java.lang.StringBuilder.append (stri ngbuilder<
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.