【问题标题】:hibernate.MappingException When Saving POJO in Hibernate Table在 Hibernate 表中保存 POJO 时出现 hibernate.MappingException
【发布时间】:2017-10-02 17:53:36
【问题描述】:

UserDO.java

@Entity
@Table(name = "UserDO")
public class UserDO {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long userId;

    private boolean successfullyLinked;

    private UserInformation userInformation;
}

UserInformation.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "address", "country_code", "currency_code", "email_address", "name", "phone" })
public class UserInformation {

    @JsonProperty("address")
    @Valid
    private Address address;

    @JsonProperty("country_code")
    @NotNull
    private String countryCode;

    @JsonProperty("currency_code")
    @Size(min = 3, max = 3)
    private String currencyCode;

    @JsonProperty("email_address")
    @NotNull
    private String emailAddress;

    @JsonProperty("name")
    @Valid
    @NotNull
    private Name name;

    @JsonProperty("phone")
    @Valid
    private Phone phone;

}

我正在尝试将 UserInformation POJO 保存为 Hibernate 中 UserDO 的一部分。但是,在将其作为 Spring Boot 应用程序的一部分运行时,出现错误。以下是堆栈跟踪。

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

原因:javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建 Hibernate SessionFactory

原因:org.hibernate.MappingException:无法确定类型:com.paypal.marketplaces.vaas.api.models.UserInformation,在表:跟踪,列:[org.hibernate.mapping.Column(userInformation )]

注意:UserInformation POJO 非常复杂,其中包含其他对象以及这些对象中的对象(等等)。任何不需要将 UserInformation POJO 显式映射到 UserDO 表的列的解决方案都是可取的。

任何帮助将不胜感激!

【问题讨论】:

    标签: java spring hibernate spring-boot jpa


    【解决方案1】:

    持久性提供者不知道该类,也不知道如何处理它。 我建议将其设为 Embeddable 并可选择指定列名:

    import javax.persistence.Embeddalbe;
    import javax.persistence.Column;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({ "address", "country_code", "currency_code", "email_address", "name", "phone" })
    @Embeddable
    public class UserInformation {
    
        @JsonProperty("country_code")
        @NotNull
        @Column(name = "COUNTRY_CODE")
        private String countryCode;
    

    您必须对每个嵌套类重复该过程。

    最后用:注释userInformation

    @Embedded
    private UserInformation userInformation;
    

    【讨论】:

    • 感谢您的回答! UserInformation 中的每个对象(例如:地址)是否也需要可嵌入?
    • 是的,他们必须是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多