Spring 启动 CrudRepository 保存 - 异常是 org.hibernate.type.SerializationException:无法序列化

Spring boot CrudRepository save - exception is org.hibernate.type.SerializationException: could not serialize

不确定为什么我在这里遇到问题,但是当我使用带有这些对象的 CrudRepository 保存时,我得到了 SerializationException(没有更多信息)。有人可以看看我的对象并让我深入了解为什么它们不能序列化吗?我的 pom.xml 也附在最后,以防万一。我正在使用 Postgres 数据库。

编辑:数据库和现在的表已创建,但对象未创建行。

实际的 CrudRepository 接口:

public interface AccountRepository extends CrudRepository<ZanyDishAccount, String> {}

ZanyDishAccount 实体:

@Entity
public class ZanyDishAccount {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;   // internal id of the customer account for a Zany Dish subscription

    private String status;

    @OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "company_id")
    private Company company;

    @OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "order_id")
    private Order order;

    public ZanyDishAccount() {}
    public ZanyDishAccount(Company company, Order order) {

        this.company = company;
        this.order = order;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+ ", company = " + company + ", status = " + status + "]";
    }

}

公司实体:

@Entity
public class Company {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    Long id;

    private String phoneNumber;

    private String website;

    private String name;

    private String uuid;

    private String country;

    public Company() {}
    public Company(String phoneNumber, String website, String name, String uuid, String country) {

        this.phoneNumber = phoneNumber;
        this.website = website;
        this.uuid = uuid;
        this.country = country;
    }

    public String getPhoneNumber ()
    {
        return phoneNumber;
    }

    public void setPhoneNumber (String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }

    public String getWebsite ()
    {
        return website;
    }

    public void setWebsite (String website)
    {
        this.website = website;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public String getUuid ()
    {
        return uuid;
    }

    public void setUuid (String uuid)
    {
        this.uuid = uuid;
    }

    public String getCountry ()
    {
        return country;
    }

    public void setCountry (String country)
    {
        this.country = country;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [phoneNumber = "+phoneNumber+", website = "+website+", name = "+name+", uuid = "+uuid+", country = "+country+"]";
    }
}

订单实体:

@Entity
@Table(name = "_order")
public class Order {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    Long id;

    private String pricingDuration;

    private Items[] items;

    private String editionCode;

    public Order() {}

    public Order(String pricingDuration, Items[] items, String editionCode) {

        this.pricingDuration = pricingDuration;
        this.items = items;
        this.editionCode = editionCode;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPricingDuration ()
    {
        return pricingDuration;
    }

    public void setPricingDuration (String pricingDuration)
    {
        this.pricingDuration = pricingDuration;
    }

    public Items[] getItems ()
    {
        return items;
    }

    public void setItems (Items[] items)
    {
        this.items = items;
    }

    public String getEditionCode ()
    {
        return editionCode;
    }

    public void setEditionCode (String editionCode)
    {
        this.editionCode = editionCode;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [pricingDuration = "+pricingDuration+", items = "+items+", editionCode = "+editionCode+"]";
    }
}

感谢您的帮助!

麦克

嗯,这好像是multi-faceted。让我们看看我是否能提供帮助。最后一件事优先...

没有自动创建表。

我会查看 Spring 文档中的这一部分以获得最基本的方法:Initialize a database using Hibernate. For example, spring.jpa.hibernate.ddl-auto: create-drop will drop and re-create tables each time the application runs. Simple and easy for initial dev work. More robust would be leveraging something like Flyway or Liquibase

序列化问题

因此,如果没有日志,并且您没有创建任何表,则可能会认为缺少持久层是罪魁祸首。也就是说,当您有表和数据时,如果您没有所有相关表的存储库,您最终会遇到 Whosebug 错误(序列化变成循环)。为此,您可以使用 @JsonBackReference (child) and @JsonManagedReference (parent)。我只使用 @JsonBackReference 就成功了 child.

项目[]

我不确定 Item.class 是什么样子,但这看起来像是我错过了第一轮的进攻配置。

  1. private Items[] items; 更改为 private List<Item> items = new ArrayList<Item>();。用 @ElementCollection.

  2. 注释
  3. @Embeddable注释Item.class。