title: infinite recursion when querying sql using repository in jpa hibernate
body: <p>I have two entity, <code>Promotion</code> which contains information about a promotion and <code>Promotion Details</code> which contains list of products in a <code>Promotion</code>. Below is my code.</p>
  
  <p><strong>Promotion Entity:</strong></p>
  
  <pre><code>@Entity
  @Data
  public class Promotion {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long id;
  
      @OneToMany(mappedBy = "promotion")
      private List&lt;PromotionDetails&gt; details;
  
      @Column(name = "start_date")
      private Date startDate;
  
      @Column(name = "end_date")
      private Date endDate;
  }
  </code></pre>
  
  <p><strong>PromotionDetails Entity:</strong></p>
  
  <pre><code>@Entity
  @IdClass(PromotionDetailsPK.class)
  @Data
  public class PromotionDetails {
  
      @Id
      @ManyToOne(optional = false)
      @JoinColumn(name = "promotion_id", referencedColumnName = "promotion_id")
      private Promotion promotion;
  
      @Id
      @ManyToOne(optional = false)
      @JoinColumn(name="product_id", referencedColumnName="product_id")
      private Product product;
  
      @Column()
      private double discount;
  
  }
  </code></pre>
  
  <p>I want to retrieve one <code>Promotion</code> which is available for a product using JPA.</p>
  
  <p><strong>PromotionRepository:</strong></p>
  
  <pre><code>@Repository
  public interface PromotionRepository extends JpaRepository&lt;Promotion, Long&gt; {
  
      @Query("SELECT promotion "
              + "FROM Promotion promotion "
              + "INNER JOIN promotion.details details "
              + "WHERE details.product.id = :productId "
              + "AND CURRENT_DATE BETWEEN promotion.startDate AND promotion.endDate")
      Promotion findOneByProductId(@Param("productId") Long productId);
  
  }
  </code></pre>
  
  <p>But this error occured:</p>
  
  <p><strong>Stacktrace:</strong></p>
  
  <pre><code>Caused by: java.lang.StackOverflowError: null
      at java.lang.StringBuilder.append(Unknown Source)
      at java.lang.StringBuilder.&lt;init&gt;(Unknown Source)
      at com.example.entity.PromotionDetails.toString(PromotionDetails.java:18)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at java.util.AbstractCollection.toString(Unknown Source)
      at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:527)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at com.example.entity.Promotion.toString(Promotion.java:18)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at com.example.entity.PromotionDetails.toString(PromotionDetails.java:18)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at java.util.AbstractCollection.toString(Unknown Source)
      at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:527)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at com.example.entity.Promotion.toString(Promotion.java:18)
      ...
      ...
      ...
      ...
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at jp.co.worksap.stm.entity.sales.PromotionDetails.toString(PromotionDetails.java:18)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at java.util.AbstractCollection.toString(Unknown Source)
      at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:527)
      at java.lang.String.valueOf(Unknown Source)
      at java.lang.StringBuilder.append(Unknown Source)
      at jp.co.worksap.stm.entity.sales.Promotion.toString(Promotion.java:18)
  </code></pre>
  
  <p>I know it seems like <code>Promotion</code> is calling <code>PromotionDetails</code> and <code>PromotionDetails</code> is calling <code>Promotion</code> and so on. But, how do I fix this?</p>
  
  <p><strong>--EDIT--</strong></p>
  
  <p>I would like to add something. I am using Lombok to generate Getter, Setter, and toString</p>