title: Spring Data JPA throwing exception
body: <p>Was using Spring data JPA for curd operations. getting <code>Exception occurred</code> message in the controller. Please find the code below for the same.Also the stack trace attached.</p>
  
  <p>Purchases class :</p>
  
  <pre><code>@Entity
  @Table(name="purchases")
  public class Purchases {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long orderId;
  
      @Temporal(TemporalType.TIMESTAMP)
      private Date creation_time;
  
  
      @Column(name="user_id")
      private Long userId;
  
      @Column(name="isbn")
      private Long isbn;
  
      @OneToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="user_id",insertable=false, updatable=false)
      private User user;
  
  
      @OneToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="isbn",insertable=false, updatable=false)
      private Books book;
      //setters and getters
  }
  </code></pre>
  
  <p>Purchases repository -</p>
  
  <pre><code>@Transactional
  public interface PurchasesRepository extends CrudRepository&lt;Purchases, Long&gt;{
  
      List&lt;Purchases&gt; findByUserId(Long userId);  
      List&lt;Purchases&gt; findByIsbn(Long isbn);
  }
  </code></pre>
  
  <p>Purchases controller -</p>
  
  <pre><code>@RequestMapping(value="/purchase",method=RequestMethod.POST)
  public @ResponseBody Purchases purchase(@RequestBody Purchases purchases)throws Exception{
      Books book=null;
      User user=null;
      Purchases purchase=null;
      Date creation_date=new Date();
  
      if(purchases.getOrderId()==null){
  
          //check whether book exists in the database 
          book=booksDAO.findOne(purchases.getIsbn());
          user=userDAO.findOne(purchases.getUserId());
          if(book==null || user==null){
              throw new Exception("Entered book isbn or user id not found !!!");          
          }
          System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Book and user exists in the databse &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;");
          System.out.println("&gt;&gt;&gt; user.getId()=["+user.getId()+"]&gt;&gt;&gt;&gt;&gt;&gt; book.getIsbn()=["+book.getIsbn());
          System.out.println("&lt;&lt;&lt; purchaseDAO.findByUserId(user.getId())="+purchaseDAO.findByUserId(user.getId())+"&lt;&lt;&lt;&lt;&lt;&lt;&lt;purchaseDAO.findByIsbn(book.getIsbn())="+purchaseDAO.findByIsbn(book.getIsbn()));
  
          //check for book already purchase by the customer
          if(purchaseDAO.findByUserId(user.getId())!=null){
              if(purchaseDAO.findByIsbn(book.getIsbn())==null){
                  purchase = new Purchases(null,creation_date,purchases.getUserId(),purchases.getIsbn());
                  purchaseDAO.save(purchase);
                  purchase.setBook(book);
                  purchase.setUser(user);
              }else{                  //checking of the isbn 
                  throw new Exception("Book already purchased !!!");              
              }
          }else{          //checking fo the user existing
              purchase = new Purchases(null,creation_date,purchases.getUserId(),purchases.getIsbn());
              purchaseDAO.save(purchase);
              purchase.setBook(book);
              purchase.setUser(user);
          }
      }else{              //first if checking 
          throw new Exception(" Order_id already used !!! ");
      }
      return purchase;
  } 
  </code></pre>
  
  <p>Purchase controller  class -</p>
  
  <pre><code>@RequestMapping(value="/purchase",method=RequestMethod.POST)
  public @ResponseBody Purchases purchase(@RequestBody Purchases purchases)throws Exception{
      Books book=null;
      User user=null;
      Purchases purchase=null;
      Date creation_date=new Date();
  
      if(purchases.getOrderId()==null){
  
          //check whether book exists in the database 
          book=booksDAO.findOne(purchases.getIsbn());
          user=userDAO.findOne(purchases.getUserId());
          if(book==null || user==null){
              throw new Exception("Entered book isbn or user id not found !!!");          
          }
          System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Book and user exists in the databse &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;");
          System.out.println("&gt;&gt;&gt; user.getId()=["+user.getId()+"]&gt;&gt;&gt;&gt;&gt;&gt; book.getIsbn()=["+book.getIsbn());
          System.out.println("&lt;&lt;&lt; purchaseDAO.findByUserId(user.getId())="+purchaseDAO.findByUserId(user.getId())+"&lt;&lt;&lt;&lt;&lt;&lt;&lt;purchaseDAO.findByIsbn(book.getIsbn())="+purchaseDAO.findByIsbn(book.getIsbn()));
  
          //check for book already purchase by the customer
          if(purchaseDAO.findByUserId(user.getId())!=null){
              if(purchaseDAO.findByIsbn(book.getIsbn())==null){
                  purchase = new Purchases(null,creation_date,purchases.getUserId(),purchases.getIsbn());
                  purchaseDAO.save(purchase);
                  purchase.setBook(book);
                  purchase.setUser(user);
              }else{                  //checking of the isbn 
                  throw new Exception("Book already purchased !!!");              
              }
          }else{          //checking fo the user existing
              purchase = new Purchases(null,creation_date,purchases.getUserId(),purchases.getIsbn());
              purchaseDAO.save(purchase);
              purchase.setBook(book);
              purchase.setUser(user);
          }
      }else{              //first if checking 
          throw new Exception(" Order_id already used !!! ");
      }
      return purchase;
  }
  </code></pre>
  
  <p>Using spring boot to run the application. Getting the exception in the third system.out line.I have tried to user The Purchases class as return type in Purchases Repository class instead of List but still no affect. Please find the stack trace.</p>
  
  <pre><code>Hibernate: select user0_.user_id as user_id1_2_0_, user0_.contact as contact2_2_0_, user0_.email as email3_2_0_, user0_.name as name4_2_0_ from user_details user0_ where user0_.user_id=?
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Book and user exists in the databse &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  &gt;&gt;&gt; user.getId()=[6]&gt;&gt;&gt;&gt;&gt;&gt; book.getIsbn()=[6
  Hibernate: select purchases0_.order_id as order_id1_1_, purchases0_.isbn as isbn3_1_, purchases0_.creation_time as creation2_1_, purchases0_.user_id as user_id4_1_ from purchases purchases0_ where purchases0_.user_id=?
  Hibernate: select purchases0_.order_id as order_id1_1_, purchases0_.isbn as isbn3_1_, purchases0_.creation_time as creation2_1_, purchases0_.user_id as user_id4_1_ from purchases purchases0_ where purchases0_.isbn=?
  &lt;&lt;&lt; purchaseDAO.findByUserId(user.getId())=Exception occurred !!!&lt;&lt;&lt;&lt;&lt;&lt;&lt;purchaseDAO.findByIsbn(book.getIsbn())=Exception occurred !!!
  Hibernate: select purchases0_.order_id as order_id1_1_, purchases0_.isbn as isbn3_1_, purchases0_.creation_time as creation2_1_, purchases0_.user_id as user_id4_1_ from purchases purchases0_ where purchases0_.user_id=?
  Hibernate: select purchases0_.order_id as order_id1_1_, purchases0_.isbn as isbn3_1_, purchases0_.creation_time as creation2_1_, purchases0_.user_id as user_id4_1_ from purchases purchases0_ where purchases0_.isbn=?
  2016-05-05 14:59:47.088 ERROR 12207 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.Exception: Book already purchased !!!] with root cause
  </code></pre>