Spring Data JPA 常见异常

混江龙づ霸主 提交于 2020-04-09 20:11:59

异常一:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class com.xxx.xxx.xxx.xxxxx] does not define an IdClass

原因:由于你在你的实体中增加了多个@Id注解

异常二:

Could not instantiate resultclass: java.lang.Long; nested exception is org.hibernate.HibernateException: Could not instantiate resultclass: java.lang.Long] with root causeorg.hibernate.HibernateException: Could not instantiate resultclass: java.lang.Long

原因:因为你使用了setResultTransformer

session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getSingleResult();

正确写法:

Long.parseLong(session.createSQLQuery(sql).getSingleResult().toString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!