Spring Data JPA抛出异常

时间:2016-05-05 09:57:01

标签: spring-boot spring-data spring-data-jpa spring-data-rest

使用Spring数据JPA进行凝乳操作。在控制器中收到Exception occurred消息。请找到相同的代码。附上堆栈跟踪。

购买课程:

@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
}

购买存储库 -

@Transactional
public interface PurchasesRepository extends CrudRepository<Purchases, Long>{

    List<Purchases> findByUserId(Long userId);  
    List<Purchases> findByIsbn(Long isbn);
}

购买控制器 -

@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(">>>>>>>>>>>> Book and user exists in the databse <<<<<<<<<<<<<<<<");
        System.out.println(">>> user.getId()=["+user.getId()+"]>>>>>> book.getIsbn()=["+book.getIsbn());
        System.out.println("<<< purchaseDAO.findByUserId(user.getId())="+purchaseDAO.findByUserId(user.getId())+"<<<<<<<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;
} 

购买控制器类 -

@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(">>>>>>>>>>>> Book and user exists in the databse <<<<<<<<<<<<<<<<");
        System.out.println(">>> user.getId()=["+user.getId()+"]>>>>>> book.getIsbn()=["+book.getIsbn());
        System.out.println("<<< purchaseDAO.findByUserId(user.getId())="+purchaseDAO.findByUserId(user.getId())+"<<<<<<<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;
}

使用spring boot运行应用程序。在第三个system.out行中获取异常。我试图将Purchases类用作Purchases Repository类中的返回类型而不是List,但仍然没有影响。请找到堆栈跟踪。

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=?
>>>>>>>>>>>> Book and user exists in the databse <<<<<<<<<<<<<<<<
>>> user.getId()=[6]>>>>>> 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=?
<<< purchaseDAO.findByUserId(user.getId())=Exception occurred !!!<<<<<<<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

1 个答案:

答案 0 :(得分:0)

第三个print语句中发生异常,因为purchase模型类是使用toString()定义的,因为打印异常消息。后来,我想出来并改变并得到了正确的结果。