2017-05-04 40 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

是的,他們必須是 –