ytibrewala
ytibrewala

Reputation: 1177

hibernate.MappingException When Saving POJO in Hibernate Table

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;

}

I am trying to save the UserInformation POJO as a part of the UserDO in Hibernate. However upon running this as part of a Spring Boot Application, I get an error. The following is the stack trace.

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)]

Note: The UserInformation POJO is quite complex, with other objects inside it and objects inside those objects (and so on). Any solution not requiring explicit mapping of the UserInformation POJO to colums of the UserDO table would be preferable.

Any help would be highly appreciated!

Upvotes: 1

Views: 170

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26492

Persistence provider is not aware of that class, neither what to do with it. I would suggest making it Embeddable and optionally specifying column names:

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;

You would have to repeat the process for every nested class.

And finally to annotate the userInformation with:

@Embedded
private UserInformation userInformation;

Upvotes: 3

Related Questions