2017-05-04 31 views
1

UserDO.javahibernate.MappingException当Hibernate的表保存POJO

@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保存为UserDO的一部分在休眠。但是,将其作为Spring Boot应用程序的一部分运行时,会出现错误。以下是堆栈跟踪。

org.springframework.beans.factory.BeanCreationException:错误创建名为 'entityManagerFactory的' 类路径资源定义[组织/ springframework的的/ boot /自动配置/ ORM/JPA/HibernateJpaAutoConfiguration.class]豆:初始化的调用方法失败;嵌套异常是javax.persistence.PersistenceException:[PersistenceUnit:默认]无法建立的SessionFactory

所致:javax.persistence.PersistenceException:[PersistenceUnit:默认]无法建立的SessionFactory

所致:有机.hibernate.MappingException:无法确定类型:com.paypal.marketplaces.vaas.api.models.UserInformation,在表:追踪,对于列:[org.hibernate.mapping.Column(userInformation)]

注意:UserInformation POJO非常复杂,其中的其他对象以及这些对象中的对象(和等等)。任何不需要将UserInformation POJO明确映射到UserDO表的列的解决方案将是优选的。

任何帮助将不胜感激!

回答

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; 
+0

感谢您的回答! UserInformation中的每个对象(例如: - Address)是否也需要可嵌入? – ytibrewala

+0

是的,他们必须是 –