我正在研究Gradle-Kotlin-Hibernate项目。我想让我的一些课程不可改变,这在Kotlin中特别容易。这适用于 @Entity
@Entity(name = "SOMETHING")
class MeetingKeychain(
val immutableProp: String
) {
// ID and others
}
感谢使用 kotlin-jpa 插件。但是相同的插件不适用于 @Embeddable 。
@Embeddable
class MeetingKeychain(
val immutableProp: String
) {
// ID and others
}
抛出以下异常:
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:119)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:64)
at org.hibernate.mapping.Component.getType(Component.java:169)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)
at org.hibernate.mapping.Property.isValid(Property.java:225)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
... 46 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:95)
... 56 more
Caused by: org.hibernate.PropertyNotFoundException: Could not locate setter method for property [com.scherule.calendaring.domain.entities.ParticipantId#id]
at org.hibernate.internal.util.ReflectHelper.findSetterMethod(ReflectHelper.java:540)
at org.hibernate.property.access.internal.PropertyAccessBasicImpl.<init>(PropertyAccessBasicImpl.java:44)
at org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl.buildPropertyAccess(PropertyAccessStrategyBasicImpl.java:27)
at org.hibernate.mapping.Property.getGetter(Property.java:299)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:143)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:46)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:42)
... 61 more
我的问题是,有什么方法吗?我希望我的 @Embeddable 类是不可变的。
答案 0 :(得分:0)
实际上这证明是可行的,与Kotlin无关。唯一相关的方法是你需要生成默认构造函数,该构造函数用于 kotlin-jpa 插件。你需要使用
@Access(AccessType.FIELD)
而
@Access(AccessType.PROPERTY)
是默认值。然后它会查找setter而不是使用字段注入。
答案 1 :(得分:0)
我猜这是休眠限制。它清楚地说Could not locate setter method for property
。因此,我们应该为他提供解决方案。唯一的方法是将val
更改为var
。
即使我们在插件配置中添加了no-arg
批注,@Embeddable
插件也无济于事:
noArg {
invokeInitializers = true
annotation("javax.persistence.Embeddable")
}