[英]JPA/Hibernate: is it obvious to create UserType when using AttributeConverter on complex class?
I'd like to map an arbitrary POJO class attribute as a String
with an AttributeConverter<SomeClass, String>
but I get this: 我想绘制任意POJO类属性的
String
与AttributeConverter<SomeClass, String>
但是我得到这个:
WARN: Could not find matching type descriptor for requested Java class [SomeClass];
警告:找不到所请求的Java类[SomeClass]的匹配类型描述符; using fallback
使用后备
Does that mean AttributeConverter
is not enough and I need to write a custom UserType
for every complex class to be able transforming it into String
and back? 这是否意味着
AttributeConverter
是不够的,我需要为每个复杂类编写一个自定义UserType
,以便能够将其转换为String
并返回?
Another solution requires @Embeddable
. 另一种解决方案需要
@Embeddable
。 Is it more suitable here? 这里更合适吗?
@Entity
public class SomePersistentClass {
@Id
int id;
@Convert(converter = SomeClassConverter.class)
public SomeClass attribute;
//...
}
public class SomeClass {
//...
}
Old question, but as of Hibernate 5.2.7 (Jan 24th, 2017) this warning changed to: 旧问题,但从Hibernate 5.2.7(2017年1月24日)开始,此警告更改为:
WARN org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry - HHH000481: Encountered Java type [class SomeClass] for which we could not locate a JavaTypeDescriptor and which does not appear to implement equals and/or hashCode.
WARN org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry - HHH000481:遇到Java类型[类SomeClass],我们找不到JavaTypeDescriptor,但似乎没有实现equals和/或hashCode。 This can lead to significant performance problems when performing equality/dirty checking involving this Java type.
执行涉及此Java类型的相等/脏检查时,这可能会导致严重的性能问题。 Consider registering a custom JavaTypeDescriptor or at least implementing equals/hashCode.
考虑注册自定义JavaTypeDescriptor或至少实现equals / hashCode。 So, there are these solutions:
所以,有这些解决方案:
To solve this, simply make sure your type implements equals and hashcode, and also annotate it with Immutable.class in case it is immutable. 要解决这个问题,只需确保你的类型实现equals和hashcode,并使用Immutable.class注释它,以防它是不可变的。
See: JavaTypeDescriptorRegistry - Could not find matching type descriptor for requested Java class 请参阅: JavaTypeDescriptorRegistry - 找不到所请求的Java类的匹配类型描述符
Define the pojo with 定义pojo
@Named @Named
implements Converter 实现转换器
Override the getAsObject and getAsString method 重写getAsObject和getAsString方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.