开发者社区> 问答> 正文

关于JPA中@Transient位置的问题?报错

最近在项目启动的时候遇到一个异常,大概如下:


10:19:50.150 [RMI TCP Connection(3)-127.0.0.1] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [jar:file:/G:/work-space/svn/src/km-fa/branches/v0.8.1/target/km-fa/WEB-INF/lib/km-platform-common-1.1-20160919.060846-7.jar!/kingmed-common-bean.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) ~[spring-beans-4.0.2.RELEASE.jar:4.0.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0.2.RELEASE.jar:4.0.2.RELEASE]



一步步undo代码发现是我改了@Transient的位置导致的,由字段上改到了get方法上,为什么做这个改动?因为在另一个实体类中,把@Transient放到字段上是报错的,而且为了项目代码风格统一就把原来老代码上的@Transient注解都放到了get方法上。但是现在启动报错,很奇怪,怎么一个实体放get方法报错,一个实体放字段报错,就上stackoverflow搜了下二者的差异,发现这段解答:

Default access type is determined by placement of @Id annotation, and it should be consistent for all fields of the entity (or hiererchy of inherited entities), unless explicitly overriden by @Access for some fields.

问题很明显了,默认情况下,@Transient的位置跟@Id注解的位置有关。也可使用@Access特别指定。


展开
收起
爱吃鱼的程序员 2020-06-08 19:48:41 697 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    不错,

    2020-06-08 19:48:53
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载