简体   繁体   English

JPA / Hibernate:在复杂类上使用AttributeConverter时创建UserType是否明显?

[英]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类属性的StringAttributeConverter<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

  1. @Named @Named

  2. implements Converter 实现转换器

  3. Override the getAsObject and getAsString method 重写getAsObject和getAsString方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JPA + Hibernate时如何注册自定义String UserType - How to register custom String UserType when using JPA + Hibernate JPA:在@Query 中使用 AttributeConverter - JPA: Using AttributeConverter in @Query 在JPA实体中使用JSON字段(实现休眠的UserType) - Using json field in jpa entity (implementing hibernate UserType) Hibernate Envers 因@Converter 和 AttributeConverter (JPA 2.1) 而失败 - Hibernate Envers fails with @Converter and AttributeConverter (JPA 2.1) 用户类型未正确加载的Hibernate / JPA实体属性 - Hibernate / JPA entity attribute with a UserType not loaded correctly JPA:在 JPA 查询中使用 AttributeConverter 自动解密 - JPA: Using AttributeConverter automatically decrypt in JPA query 当在复合键中使用时,JPA AttributeConverter 在 Spring-data/hibernate 查询中不适用 - JPA AttributeConverter not honored in a Spring-data/hibernate query when used in a composite key 使用Eclipse时多次注册AttributeConverter类 - AttributeConverter class registered multiple times when using Eclipse 如何使用usertype在JPA中使用Hibernate持久保存JSR 310 java.time.LocalDateTime - How to persist JSR 310 java.time.LocalDateTime in JPA with Hibernate using usertype 如果在JOIN中使用自定义查询,则Hibernate AttributeConverter会失败 - Hibernate AttributeConverter fails if using custom query with JOIN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM