Skip to content
Advertisement

StackOverflowError while fetching OneToMany mapped entity in Spring Data Jpa

I have two entities that have bidirectional mapping. The entities are given below,

**Entity 1**
@Entity
@Table(name = "Customer")
public class CustomerEntity implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /** The first name. */
    @Column(name = "FirstName", length = 60, nullable = false)
    private String firstName;

    /** The last name. */
    @Column(name = "LastName", length = 60, nullable = false)
    private String lastName;

    /** The dob. */
    @Column(name = "DOB", nullable = false)
    private LocalDate dob;

    /** The mobile number. */
    @Column(name = "MobileNumber", length = 10, nullable = false, unique = true)
    private Long mobileNumber;

    /** The email id. */
    @Column(name = "Email", length = 100, nullable = false, unique = true)
    private String emailId;

    /** The address. */
    @Column(name = "Address", length = 255)
    private String address;

    /** The created time. */
    @Column(name = "CreatedTime", nullable = false)
    private LocalDateTime createdTime;

    /** The accounts. */
    @OneToMany(mappedBy = "customerEntity", cascade = CascadeType.ALL)
    private List<AccountEntity> accountEntities = new ArrayList<>();

    // Constructors, getters, setters, hashcode, equals, and toString methods are here
    @Override
    public String toString() {
        return "CustomerEntity [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob
                + ", mobileNumber=" + mobileNumber + ", emailId=" + emailId + ", address=" + address + ", createdTime="
                + createdTime + ", accountEntities=" + accountEntities + "]";
    }

}
**Entity 2**
@Entity
@Table(name = "Account")
public class AccountEntity implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /** The account number. */
    @Column(name = "AccountNumber", length = 20, nullable = false, unique = true)
    private String accountNumber;

    /** The branch. */
    @Column(name = "Branch", nullable = false)
    private String branch;

    /** The account type. */
    @Column(name = "AccountType", nullable = false)
    private String accountType;

    /** The balance. */
    @Column(name = "Balance", nullable = false)
    private Double balance;

    /** The created time. */
    @Column(name = "CreatedTime", nullable = false)
    private LocalDateTime createdTime;

    /** The account holder. */
    @ManyToOne(fetch = FetchType.LAZY)
    private CustomerEntity customerEntity;

   // Constructors, getters, setters, hashcode, equals, and toString methods are here

   @Override
    public String toString() {
        return "AccountEntity [id=" + id + ", accountNumber=" + accountNumber + ", branch=" + branch + ", accountType="
                + accountType + ", balance=" + balance + ", createdTime=" + createdTime + ", customerEntity="
                + customerEntity + "]";
    }
}

I need to fetch all the customer entities along with its mapped accounts. My service code looks like this,

public Set<CustomerDTO> fetchCustomers(String searchKey) {
        List<CustomerEntity> customers = null;
        if (searchKey == null || searchKey.trim().isEmpty()) {
            **customers = customerRepository.findAll();**
        } else {
            **customers = customerRepository.findByFirstNameContainingIgnoreCaseOrLastNameContainingIgnoreCase(searchKey,
                    searchKey);**
        }
        System.out.println(customers);
        return customers.stream()
                .map(entity -> new CustomerDTO(entity.getId(), entity.getFirstName(), entity.getLastName(),
                        entity.getDob(), entity.getEmailId(), entity.getMobileNumber(), entity.getAddress(),
                        entity.getCreatedTime()))
                .collect(Collectors.toSet());
    }

But I am getting StackOverFlowError. The log is mentioned below,

Hibernate: select customeren0_.id as id1_1_, customeren0_.address as address2_1_, customeren0_.created_time as created_3_1_, customeren0_.dob as dob4_1_, customeren0_.email as email5_1_, customeren0_.first_name as first_na6_1_, customeren0_.last_name as last_nam7_1_, customeren0_.mobile_number as mobile_n8_1_ from customer customeren0_ where customeren0_.mobile_number=? or customeren0_.email=?
Hibernate: insert into customer (id, address, created_time, dob, email, first_name, last_name, mobile_number) values (null, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select customeren0_.id as id1_1_0_, customeren0_.address as address2_1_0_, customeren0_.created_time as created_3_1_0_, customeren0_.dob as dob4_1_0_, customeren0_.email as email5_1_0_, customeren0_.first_name as first_na6_1_0_, customeren0_.last_name as last_nam7_1_0_, customeren0_.mobile_number as mobile_n8_1_0_ from customer customeren0_ where customeren0_.id=?
Hibernate: select accountent0_.id as id1_0_, accountent0_.account_number as account_2_0_, accountent0_.account_type as account_3_0_, accountent0_.balance as balance4_0_, accountent0_.branch as branch5_0_, accountent0_.created_time as created_6_0_, accountent0_.customer_entity_id as customer7_0_ from account accountent0_ left outer join customer customeren1_ on accountent0_.customer_entity_id=customeren1_.id where customeren1_.id=? and accountent0_.account_type=?
Hibernate: insert into account (id, account_number, account_type, balance, branch, created_time, customer_entity_id) values (null, ?, ?, ?, ?, ?, ?)
Hibernate: select customeren0_.id as id1_1_, customeren0_.address as address2_1_, customeren0_.created_time as created_3_1_, customeren0_.dob as dob4_1_, customeren0_.email as email5_1_, customeren0_.first_name as first_na6_1_, customeren0_.last_name as last_nam7_1_, customeren0_.mobile_number as mobile_n8_1_ from customer customeren0_ where upper(customeren0_.first_name) like upper(?) escape ? or upper(customeren0_.last_name) like upper(?) escape ?
Hibernate: select accountent0_.customer_entity_id as customer7_0_0_, accountent0_.id as id1_0_0_, accountent0_.id as id1_0_1_, accountent0_.account_number as account_2_0_1_, accountent0_.account_type as account_3_0_1_, accountent0_.balance as balance4_0_1_, accountent0_.branch as branch5_0_1_, accountent0_.created_time as created_6_0_1_, accountent0_.customer_entity_id as customer7_0_1_ from account accountent0_ where accountent0_.customer_entity_id=?
[2m2021-09-03 14:26:18.673[0;39m [31mERROR[0;39m [35m5812[0;39m [2m---[0;39m [2m[nio-8080-exec-4][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet]   [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause

java.lang.StackOverflowError: null
    at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:582) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:175) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:87) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:112) ~[na:na]
    at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:127) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:245) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.CustomerEntity.toString(CustomerEntity.java:297) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at com.assignment.bank.entities.AccountEntity.toString(AccountEntity.java:247) ~[classes/:na]
    at java.base/java.lang.String.valueOf(String.java:3365) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:169) ~[na:na]
    at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[na:na]
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]

Can anyone help?? What am I missing here?

UPDATE toString() and log updated. Couldn’t add the full log due to size restriction.

Advertisement

Answer

on AccountEntity.class

 @Override
    public String toString() {
        return "AccountEntity [id=" + id + ", accountNumber=" + accountNumber + ", branch=" + branch + ", accountType="
                + accountType + ", balance=" + balance + ", createdTime=" + createdTime + ", customerEntity="
                + customerEntity + "]";
    }

your toString() includes customerEntity but in CustomerEntity.class there is also a field of accountEntities<AccountEntity>.

So your .toString() method get’s to a recursive call of .toString() which in the end causes the stackoverflow error.

IMO .toString() on AccountEntity.class should be

 @Override
    public String toString() {
        return "AccountEntity [id=" + id + ", accountNumber=" + accountNumber + ", branch=" + branch + ", accountType="
                + accountType + ", balance=" + balance + ", createdTime=" + createdTime + ", customerEntityId="
                + customerEntity.getId() + "]";
    }
4 People found this is helpful
Advertisement