gpt4 book ai didi

java - @Embeddables 的 @ElementCollection 每个都有 @Embeddable 字段会导致 "Could not find a setter"异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:25 27 4
gpt4 key购买 nike

我有实体 Ride 和 @ElementCollection(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;
}

}

看起来不错,但是这个配置给了我一个异常“无法在类 Location 中找到属性地址的 setter ”。

这对我来说看起来很奇怪,因为 Location 的任何属性都不需要 setter ,直到我尝试在另一个 @Embeddable 中使用它
是 ElementCollection 的目标。

即无法正确加载集合内的第二级@embeddable。

UPD 堆栈跟踪如下所示:

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)

最佳答案

我很确定它在提示,因为你有一个带有 getter 的字段 Address,但没有 setter。 JPA 足够“聪明”,可以假设以“is”和“get”开头的方法也需要相同 val 的“set”函数。因此添加一个“setAddress”,它就会停止提示。

要在不创建 setter 的情况下修复此问题,只需将该方法标记为“@Transient”即可。确保导入 javax.persistence.Transient 而不是 java.beans.Transient,否则仍然会失败。

关于java - @Embeddables 的 @ElementCollection 每个都有 @Embeddable 字段会导致 "Could not find a setter"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30160433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com