org.hibernate.InstantiationException: No default constructor for entity

描述:在idea开发springboot项目,在调用继承了JpaRepository的dao的方法时,出现异常


代码:

/**
 * @description: 用户关注项目,若未关注则向数据库插入数据,返回“OK”;已关注则从数据库删除数据,返回“concerned”
 * @param: userIdprojectId
 * @return: String
 * @author: Kanject
 */
@Override
public String concernProject(Integer userId, Integer projectId) {

   List<Integer> userConcernList = userConcernDao.concernedOrNot(userId, projectId);
   if (userConcernList.size() >= 1) {
      userConcernDao.delete(userConcernList.get(0));
      return "concerned";
   } else {
      userConcernDao.save(new UserConcern(userConcernList.get(0), userId, projectId));
      return "OK";
   }

}


异常信息:

Hibernate: SELECT id FROM t_user_concern WHERE user_id=? AND project_id=?
Hibernate: select userconcer0_.id as id1_2_0_, userconcer0_.project_id as project_2_2_0_, userconcer0_.user_id as user_id3_2_0_ from t_user_concern userconcer0_ where userconcer0_.id=?
2018-03-27 16:50:24.270  INFO 19184 --- [nio-8080-exec-3] o.h.e.internal.DefaultLoadEventListener  : HHH000327: Error performing load command : org.hibernate.InstantiationException: No default constructor for entity:  : com.zcw.entity.UserConcern
2018-03-27 16:50:24.320 ERROR 19184 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: No default constructor for entity:  : com.zcw.entity.UserConcern; nested exception is org.hibernate.InstantiationException: No default constructor for entity:  : com.zcw.entity.UserConcern] with root cause


org.hibernate.InstantiationException: No default constructor for entity:  : com.zcw.entity.UserConcern


分析:注意No default constructor for entity,UserConcern类没有默认的构造函数


解决方法:为UserConcern加上默认的构造函数即可

public UserConcern() {}


猜你喜欢

转载自blog.csdn.net/weixin_39274753/article/details/79715721