Spring Hibernate Bidirectional ManyToMany StackOverflowError2019 Community Moderator ElectionHibernate ManyToMany with Join table problems on updateDeleting from ManyToMany with IndexColumnjpa deleting manytomany linksWhat's the difference between @Component, @Repository & @Service annotations in Spring?What's the difference between JPA and Hibernate?Hibernate: ManyToMany inverse DeleteJPA (with Hibernate) @ManyToMany @JoinTable relationship cascadeMappingJackson2HttpMessageConverter Can not find a (Map) Key deserializer for typeHibernate : Why FetchType.LAZY-annotated collection property eagerly loading?show details of all orders placed by customer

CLI: Get information Ubuntu releases

How are passwords stolen from companies if they only store hashes?

Would this string work as string?

"Marked down as someone wanting to sell shares." What does that mean?

Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?

Do I need an EFI partition for each 18.04 ubuntu I have on my HD?

What is the reasoning behind standardization (dividing by standard deviation)?

How can a new country break out from a developed country without war?

Weird lines in Microsoft Word

pipe commands inside find -exec?

Extraneous elements in "Europe countries" list

Can "few" be used as a subject? If so, what is the rule?

10 year ban after applying for a UK student visa

What is the difference between something being completely legal and being completely decriminalized?

Exit shell with shortcut (not typing exit) that closes session properly

Nested Dynamic SOQL Query

UK Tourist Visa- Enquiry

Determine voltage drop over 10G resistors with cheap multimeter

Jem'Hadar, something strange about their life expectancy

Is xar preinstalled on macOS?

Why doesn't the fusion process of the sun speed up?

Do people actually use the word "kaputt" in conversation?

is this saw blade faulty?

Air travel with refrigerated insulin



Spring Hibernate Bidirectional ManyToMany StackOverflowError



2019 Community Moderator ElectionHibernate ManyToMany with Join table problems on updateDeleting from ManyToMany with IndexColumnjpa deleting manytomany linksWhat's the difference between @Component, @Repository & @Service annotations in Spring?What's the difference between JPA and Hibernate?Hibernate: ManyToMany inverse DeleteJPA (with Hibernate) @ManyToMany @JoinTable relationship cascadeMappingJackson2HttpMessageConverter Can not find a (Map) Key deserializer for typeHibernate : Why FetchType.LAZY-annotated collection property eagerly loading?show details of all orders placed by customer










0















I'm trying to build bidirectional ManyToMany association.



So, I have an entity called User:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany(mappedBy="users")
private List<Chat> chats = new ArrayList<>();



And another one called Chat:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany
@JoinTable(name = "chat_user",
joinColumns = @JoinColumn(name = "fk_chat") ,
inverseJoinColumns = @JoinColumn(name = "fk_user") )
private List<User> users = new ArrayList<>();



So, when I'm trying to do:



Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!


Getting this one:



Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()


I think the problem is: Chat has user, that refences to that Chat that has user that references to that Chat again and so on.



So how can I solve it?










share|improve this question






















  • paste methods getUsers, getChats

    – Peter Šály
    Mar 7 at 22:59















0















I'm trying to build bidirectional ManyToMany association.



So, I have an entity called User:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany(mappedBy="users")
private List<Chat> chats = new ArrayList<>();



And another one called Chat:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany
@JoinTable(name = "chat_user",
joinColumns = @JoinColumn(name = "fk_chat") ,
inverseJoinColumns = @JoinColumn(name = "fk_user") )
private List<User> users = new ArrayList<>();



So, when I'm trying to do:



Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!


Getting this one:



Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()


I think the problem is: Chat has user, that refences to that Chat that has user that references to that Chat again and so on.



So how can I solve it?










share|improve this question






















  • paste methods getUsers, getChats

    – Peter Šály
    Mar 7 at 22:59













0












0








0








I'm trying to build bidirectional ManyToMany association.



So, I have an entity called User:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany(mappedBy="users")
private List<Chat> chats = new ArrayList<>();



And another one called Chat:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany
@JoinTable(name = "chat_user",
joinColumns = @JoinColumn(name = "fk_chat") ,
inverseJoinColumns = @JoinColumn(name = "fk_user") )
private List<User> users = new ArrayList<>();



So, when I'm trying to do:



Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!


Getting this one:



Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()


I think the problem is: Chat has user, that refences to that Chat that has user that references to that Chat again and so on.



So how can I solve it?










share|improve this question














I'm trying to build bidirectional ManyToMany association.



So, I have an entity called User:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany(mappedBy="users")
private List<Chat> chats = new ArrayList<>();



And another one called Chat:



import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@ManyToMany
@JoinTable(name = "chat_user",
joinColumns = @JoinColumn(name = "fk_chat") ,
inverseJoinColumns = @JoinColumn(name = "fk_user") )
private List<User> users = new ArrayList<>();



So, when I'm trying to do:



Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!


Getting this one:



Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()


I think the problem is: Chat has user, that refences to that Chat that has user that references to that Chat again and so on.



So how can I solve it?







spring hibernate spring-boot jpa many-to-many






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 18:36









Nick KuleseNick Kulese

155




155












  • paste methods getUsers, getChats

    – Peter Šály
    Mar 7 at 22:59

















  • paste methods getUsers, getChats

    – Peter Šály
    Mar 7 at 22:59
















paste methods getUsers, getChats

– Peter Šály
Mar 7 at 22:59





paste methods getUsers, getChats

– Peter Šály
Mar 7 at 22:59












1 Answer
1






active

oldest

votes


















1














Yes, you are right with never-ending recursion.



How do we solve this problem? Can try these steps



1 . If you have used toString() in mentioned entities, remove reference of other entity from it.



2 . Can add @JsonIgnore at one side of relation which will break the chain



 @ManyToMany(mappedBy="users")
@JsonIgnore
private List<Chat> chats = new ArrayList<>();


Refer to this article for more ways



3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.






share|improve this answer























  • Thank's a lot! It all works now :)

    – Nick Kulese
    Mar 8 at 8:02










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050640%2fspring-hibernate-bidirectional-manytomany-stackoverflowerror%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Yes, you are right with never-ending recursion.



How do we solve this problem? Can try these steps



1 . If you have used toString() in mentioned entities, remove reference of other entity from it.



2 . Can add @JsonIgnore at one side of relation which will break the chain



 @ManyToMany(mappedBy="users")
@JsonIgnore
private List<Chat> chats = new ArrayList<>();


Refer to this article for more ways



3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.






share|improve this answer























  • Thank's a lot! It all works now :)

    – Nick Kulese
    Mar 8 at 8:02















1














Yes, you are right with never-ending recursion.



How do we solve this problem? Can try these steps



1 . If you have used toString() in mentioned entities, remove reference of other entity from it.



2 . Can add @JsonIgnore at one side of relation which will break the chain



 @ManyToMany(mappedBy="users")
@JsonIgnore
private List<Chat> chats = new ArrayList<>();


Refer to this article for more ways



3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.






share|improve this answer























  • Thank's a lot! It all works now :)

    – Nick Kulese
    Mar 8 at 8:02













1












1








1







Yes, you are right with never-ending recursion.



How do we solve this problem? Can try these steps



1 . If you have used toString() in mentioned entities, remove reference of other entity from it.



2 . Can add @JsonIgnore at one side of relation which will break the chain



 @ManyToMany(mappedBy="users")
@JsonIgnore
private List<Chat> chats = new ArrayList<>();


Refer to this article for more ways



3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.






share|improve this answer













Yes, you are right with never-ending recursion.



How do we solve this problem? Can try these steps



1 . If you have used toString() in mentioned entities, remove reference of other entity from it.



2 . Can add @JsonIgnore at one side of relation which will break the chain



 @ManyToMany(mappedBy="users")
@JsonIgnore
private List<Chat> chats = new ArrayList<>();


Refer to this article for more ways



3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 4:56









MyTwoCentsMyTwoCents

3,1542930




3,1542930












  • Thank's a lot! It all works now :)

    – Nick Kulese
    Mar 8 at 8:02

















  • Thank's a lot! It all works now :)

    – Nick Kulese
    Mar 8 at 8:02
















Thank's a lot! It all works now :)

– Nick Kulese
Mar 8 at 8:02





Thank's a lot! It all works now :)

– Nick Kulese
Mar 8 at 8:02



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050640%2fspring-hibernate-bidirectional-manytomany-stackoverflowerror%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229