StackOverflowError 添加用户字段后发生。

huangapple go评论106阅读模式
英文:

StackOverflowError after adding user field

问题

你的代码中似乎存在循环引用的问题,导致栈溢出错误。具体来说,你的User类和Role类都定义了一个Set类型的字段,分别是rolesusers,并且它们使用了@ManyToMany注解来表示它们之间的多对多关系。这会导致在加载数据时出现循环引用,从而导致栈溢出错误。

为了解决这个问题,你可以考虑以下几种方法之一:

  1. 移除User类中的roles字段,因为你已经在Role类中定义了与用户的关联。这可以防止循环引用。

  2. User类的roles字段上使用@JsonIgnore注解,以防止在序列化时引发循环引用。这对于REST API等情况可能有帮助。

  3. 考虑在需要时使用懒加载来加载roles,以避免在一次加载中加载所有相关实体。这可能需要对你的数据访问层进行一些调整。

无论你选择哪种方法,都需要确保在数据模型中避免循环引用,以免导致栈溢出错误。同时,你还可以检查你的数据库模式,确保user_to_role表的关联正确设置。

英文:
  1. @Data
  2. @Entity
  3. @Table(name = "users")
  4. @ToString(exclude = "password")
  5. @NoArgsConstructor
  6. public class User {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.IDENTITY)
  9. private Long id;
  10. @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE})
  11. @JoinTable(name = "user_to_role",
  12. joinColumns = @JoinColumn(name = "user_id"),
  13. inverseJoinColumns = @JoinColumn(name = "role_id"))
  14. private Set<Role> roles;
  15. }

in my code users have a role field, I decided to add users field to the roles entity.I need this in order not to contact the database every time to search for users, if there are roles. I want to get a list of id of users who have this role, without the rest of the fields. As I understand it, lazy loading will help me with this, but I'm not sure(I haven't had time to test yet).
When I try to log in, I get the following error, I suppose it is due to spring security, I don't understand

  1. @Data
  2. @Entity
  3. @Table(name = "roles")
  4. @NoArgsConstructor
  5. public class Role {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private Long id;
  9. @ManyToMany
  10. @JoinTable(name = "user_to_role",
  11. joinColumns = @JoinColumn(name = "user_id"),
  12. inverseJoinColumns = @JoinColumn(name = "role_id"))
  13. private Set<User> users;
  14. }
  15. java.lang.StackOverflowError: null
  16. at ch.qos.logback.classic.spi.TurboFilterList.getTurboFilterChainDecision(TurboFilterList.java:49) ~[logback-classic-1.2.3.jar:na]
  17. at ch.qos.logback.classic.LoggerContext.getTurboFilterChainDecision_0_3OrMore(LoggerContext.java:269) ~[logback-classic-1.2.3.jar:na]
  18. at ch.qos.logback.classic.Logger.callTurboFilters(Logger.java:751) ~[logback-classic-1.2.3.jar:na]
  19. at ch.qos.logback.classic.Logger.isDebugEnabled(Logger.java:469) ~[logback-classic-1.2.3.jar:na]
  20. at org.apache.logging.slf4j.SLF4JLogger.isEnabledFor(SLF4JLogger.java:211) ~[log4j-to-slf4j-2.13.3.jar:2.13.3]
  21. at org.apache.logging.slf4j.SLF4JLogger.isEnabled(SLF4JLogger.java:121) ~[log4j-to-slf4j-2.13.3.jar:2.13.3]
  22. at org.apache.logging.log4j.spi.AbstractLogger.isEnabled(AbstractLogger.java:1513) ~[log4j-api-2.13.3.jar:2.13.3]
  23. at org.jboss.logging.Log4j2Logger.doLog(Log4j2Logger.java:52) ~[jboss-logging-3.4.1.Final.jar:3.4.1.Final]
  24. at org.jboss.logging.Logger.debug(Logger.java:531) ~[jboss-logging-3.4.1.Final.jar:3.4.1.Final]
  25. at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:128) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  26. at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:112) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  27. at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:170) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  28. at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  29. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:198) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  30. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:162) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  31. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:104) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  32. at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  33. at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:710) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  34. at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  35. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  36. at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2161) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  37. at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  38. at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  39. at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  40. at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  41. at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  42. at dev.redlab.mgs.backoffice.model.entities.Role.hashCode(Role.java:25) ~[main/:na]
  43. at java.base/java.util.HashMap.hash(HashMap.java:340) ~[na:na]
  44. at java.base/java.util.HashMap.put(HashMap.java:613) ~[na:na]
  45. at java.base/java.util.HashSet.add(HashSet.java:221) ~[na:na]
  46. at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336) ~[na:na]
  47. at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  48. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  49. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  50. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  51. at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  52. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:267) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  53. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  54. at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:137) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  55. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  56. at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  57. at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:710) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  58. at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  59. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  60. at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2161) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  61. at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  62. at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  63. at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  64. at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  65. at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  66. at dev.redlab.mgs.backoffice.model.entities.User.hashCode(User.java:15) ~[main/:na]
  67. at java.base/java.util.HashMap.hash(HashMap.java:340) ~[na:na]
  68. at java.base/java.util.HashMap.put(HashMap.java:613) ~[na:na]
  69. at java.base/java.util.HashSet.add(HashSet.java:221) ~[na:na]
  70. at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336) ~[na:na]
  71. at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  72. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  73. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  74. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  75. at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  76. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:267) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  77. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  78. at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:137) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  79. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  80. at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  81. at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:710) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  82. at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  83. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  84. at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2161) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  85. at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  86. at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  87. at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  88. at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  89. at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  90. at dev.redlab.mgs.backoffice.model.entities.Role.hashCode(Role.java:25) ~[main/:na]
  91. at java.base/java.util.HashMap.hash(HashMap.java:340) ~[na:na]
  92. at java.base/java.util.HashMap.put(HashMap.java:613) ~[na:na]
  93. at java.base/java.util.HashSet.add(HashSet.java:221) ~[na:na]
  94. at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336) ~[na:na]
  95. at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  96. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  97. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  98. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  99. at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  100. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:267) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  101. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  102. at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:137) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  103. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  104. at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  105. at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:710) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  106. at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  107. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  108. at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2161) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  109. at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  110. at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  111. at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  112. at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  113. at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  114. at dev.redlab.mgs.backoffice.model.entities.User.hashCode(User.java:15) ~[main/:na]
  115. at java.base/java.util.HashMap.hash(HashMap.java:340) ~[na:na]
  116. at java.base/java.util.HashMap.put(HashMap.java:613) ~[na:na]
  117. at java.base/java.util.HashSet.add(HashSet.java:221) ~[na:na]
  118. at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336) ~[na:na]
  119. at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  120. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  121. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  122. at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  123. at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  124. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:267) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  125. at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  126. at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:137) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  127. at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  128. at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  129. at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:710) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  130. at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  131. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  132. at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2161) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  133. at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  134. at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  135. at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  136. at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
  137. at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]

this is repeated a huge number of times, I did not insert. thanks in advance
there is a user_to_role table in the database and an entry in it

答案1

得分: 0

  1. @JsonIgnore
  2. @ManyToMany
  3. @JoinTable(
  4. name = "user_to_role",
  5. joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
  6. inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
  7. )
  8. private Set<Role> roles;

在User实体中尝试像这样,并在Role实体中进行相反操作(根据实体进行join和inverseJoin)。

请确保至少一个实体以懒加载模式加载/获取此属性,否则将出现无限循环的引用准备获取的情况。

  1. @ManyToMany(fetch = FetchType.LAZY)

上述操作应该在至少一个相互引用的实体上完成。

英文:
  1. @JsonIgnore
  2. @ManyToMany
  3. @JoinTable(
  4. name = &quot;user_to_role&quot;,
  5. joinColumns = {@JoinColumn(name = &quot;user_id&quot;, referencedColumnName = &quot;id&quot;)},
  6. inverseJoinColumns = {@JoinColumn(name = &quot;role_id&quot;, referencedColumnName = &quot;id&quot;)})
  7. private Set&lt;Role&gt; roles;

Can you try like this in the User entity, and the Vice Versa(inverseJoin and join based on entity) on Role Entity side also.

And please ensure that either of the entity should load/Fetch this property in a Lazy mode otherwise there will be an infinite loop of references that are ready to fetched.

  1. @ManyToMany(fetch = FetchType.LAZY)

The above should be done on at least one of the cross referenced entities.

答案2

得分: 0

我的代码使用注解 @Data,该注解生成 hashCode 和 ToString 方法,而这个结构导致了 Hibernate 卡住。在我的情况下,我只是添加了排除参数,比如 @EqualsAndHashCode(exclude="dependent_list")

英文:

My code uses the annotation @Data, which generates hashCode and ToString methods and this structure leads to Hibernate stuck. in my case I just added the exclusion parameters, like @EqualsAndHashCode(exclude=&quot;dependent_list&quot;)

huangapple
  • 本文由 发表于 2020年8月1日 06:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63199764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定