我有@ElementCollection的实体Ride(targetClass = RideLocation.class):
@ElementCollection(targetClass = RideLocation.class)
@CollectionTable(name = "ride_locations", joinColumns = @JoinColumn(name = "ride_id"))
List<RideLocation> locations = new ArrayList<>();
RideLocation是@Embeddable,其中包含另一个@Embeddable:
@Embeddable
public class RideLocation {
@Column(name = "timestamp")
long timestamp;
@Embedded
Location location;
public RideLocation(long timestamp, Location location) {
Assert.isTrue(timestamp > 0);
Assert.notNull(location);
this.timestamp = timestamp;
this.location = location;
}
RideLocation() {
}
public long getTimestamp() {
return timestamp;
}
public Location getLocation() {
return location;
}
}
位置是@Embeddable本身:
@Embeddable
public class Location {
@Column(name = "address")
String address;
@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name = "point", columnDefinition = "geometry")
Point point;
public Location(double lat, double lng, String address) {
this.point = wktToPoint(String.format("POINT(%.6f %.6f)", lng, lat));
this.address = address;
}
Location() {
}
public String getAddress() {
return address;
}
public Point getPoint() {
return point;
}
@Transient
public double getLat() {
return point.getY();
}
@Transient
public double getLng() {
return point.getX();
}
private Point wktToPoint(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
geom.setSRID(4326);
return (Point) geom;
}
}
看起来很不错,但是这个配置给了我一个例外“无法在类位置找到属性地址的setter”。
这对我来说很奇怪,因为在我试图在另一个@Embeddable中使用它之前,Location不需要任何属性的setter 是ElementCollection的目标。
即。第二级@embeddable内部集合无法正确加载。
UPD stacktrace如下所示:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.dropbyke.application.JpaConfig: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
at com.dropbyke.web.api.DropbykeApi.main(DropbykeApi.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:101)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:122)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:81)
at org.hibernate.mapping.Component.getType(Component.java:180)
at org.hibernate.tuple.PropertyFactory.buildStandardProperty(PropertyFactory.java:353)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:71)
at org.hibernate.mapping.Component.getType(Component.java:180)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
at org.hibernate.mapping.Collection.validate(Collection.java:315)
at org.hibernate.mapping.Set.validate(Set.java:40)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 21 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.GeneratedConstructorAccessor28.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1002)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
... 41 common frames omitted
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property address in class com.dropbyke.domain.Location
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:246)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:240)
at org.hibernate.mapping.Property.getSetter(Property.java:328)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildSetter(PojoComponentTuplizer.java:159)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:65)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:59)
答案 0 :(得分:0)
我很确定它会抱怨,因为你有一个带有吸气剂的现场地址,而且没有制定者。 JPA是&#34; smart&#34;足以假设以&#34;开头的方法是&#34;并且&#34;得到&#34;还需要&#34;设置&#34;功能相同的val。所以添加一个&#34; setAddress&#34;并且它会停止抱怨。
要在不设置setter的情况下解决此问题,只需将方法标记为&#34; @ Transient&#34;。确保导入javax.persistence.Transient,而不是java.beans.Transient,或者它仍然失败。