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
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
add a comment |
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
paste methods getUsers, getChats
– Peter Šály
Mar 7 at 22:59
add a comment |
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
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
spring hibernate spring-boot jpa many-to-many
asked Mar 7 at 18:36
Nick KuleseNick Kulese
155
155
paste methods getUsers, getChats
– Peter Šály
Mar 7 at 22:59
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
Thank's a lot! It all works now :)
– Nick Kulese
Mar 8 at 8:02
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thank's a lot! It all works now :)
– Nick Kulese
Mar 8 at 8:02
add a comment |
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.
Thank's a lot! It all works now :)
– Nick Kulese
Mar 8 at 8:02
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
paste methods getUsers, getChats
– Peter Šály
Mar 7 at 22:59