- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有实体 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/
我想知道是否可以在另一个 Embeddable 中包含一个 Embeddable 的 ElementCollection ? 这是我的供应商实体的示例,其中包含可嵌入类型的地址列表: @Entity
我们正在将旧的基于 xml 的配置转换为基于注解的配置 情况 有一个类注释为 @Embeddable(ParentPk.java) ,另一个类扩展了这个类,它是 @Embeddable(ChildPk
JPA ElementCollection Embeddable 可以具有 ElementCollection List Embeddable 属性吗? @Entity class Perso
我有实体 Ride 和 @ElementCollection(targetClass = RideLocation.class): @ElementCollection(targetClass = R
在搜索了 JPA 文档和各种帖子后,我对 JPA2.0 是否可以实现以下功能感到困惑。我刚开始使用 JPA,所以如果我做了一些愚蠢的事情,请原谅, 我的领域模型有一个“投资组合”,其中包含零个或多个“
我正在使用 @ElementCollection 作为自定义对象列表 @ElementCollection @CollectionTable(name = "choice", joinColumns
我正在使用 v3 youtube API 根据其 ID 获取视频数据。 这是我当前的搜索: https://www.googleapis.com/youtube/v3/videos?part=snip
两个表如: CREATE TABLE foo ( id INT PRIMARY KEY, x TEXT ); CREATE TABLE bar ( foo_id INT REFERENCE
想象一下下面的类 @Embeddable class A { @ManyToOne public B classB; ... public State someEnum
我收到了一个 Eclipse 错误:@EmbeddedId . 这是实体: @Entity @Table(name = "PERSON") public class Person implements
我按照 sitepoint moviestore tutorial进行操作,除了用户购买电影后未将视频加载到iframe中之外,其他一切工作正常。我在一栏中有一个带有youtube url的csv文件
我有一个简单的关系,其中一个实体有许多特定于它的地址,定义为: @Entity public class Corporation { @Id private Long id;
嵌入式和实体之间是否可能存在双向关系或仅存在单向关系? @Entity public class Employee { @Id private long id; ... @Embedd
我看到许多使用 @Embeddable 建立一对多关系的示例,如下所示: @Entity @Table(name = "profiles") public class Profile { @I
如何获取 Maven 插件内的 Maven 项目的所有配置文件列表? 会是: MavenEmbedder.readModel(new File("path to pom.xml")).getProfi
我尝试在可嵌入中使用 elementCollection,但 JPA2.0 似乎忽略它。 如果我进行调试,我会看到可嵌入的集合已被填充,但它不会被持久化,并且 JPA 不会抛出任何异常。它似乎只是由于
我想创建一个类 Exporter,它有两个电话号码:座机和手机。知道电话号码由国家代码和号码组成,我创建了一个 Embeddable 类: @Embeddable public class Embed
我有一个场景,hibernate 中的 Embeddable 类使用Entity。根据我在SO和其他链接上找到的各种答案,我们可以在Embeddable类中编写@ManyToOne,@OneToMan
我有一个用户类,它有一个类配置文件@Embedded。如何为 Profile 的实例提供对其所有者 User 类的引用? @Entity class User implements Serializa
我有一个 JPA 实体的类层次结构,它们都继承自 BaseEntity 类: @MappedSuperclass @EntityListeners( { ValidatorListener.class
我是一名优秀的程序员,十分优秀!