gpt4 book ai didi

java - 在 Hibernate 表中保存 POJO 时发生 hibernate.MappingException

转载 作者:行者123 更新时间:2023-11-30 02:33:03 25 4
gpt4 key购买 nike

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: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

Caused by: org.hibernate.MappingException: Could not determine type for: com.paypal.marketplaces.vaas.api.models.UserInformation, at table: Tracking, for columns: [org.hibernate.mapping.Column(userInformation)]

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

任何帮助将不胜感激!

最佳答案

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

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;

关于java - 在 Hibernate 表中保存 POJO 时发生 hibernate.MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43784786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com