1
回答
关于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特别指定。


<无标签>
举报
顶部