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

时间:2017-08-02 23:37:13

标签: postgresql hibernate spring-boot spring-data-jpa entities

我不知道为什么我在这里遇到问题,但是当我使用带有这些对象的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+"]";
    }
}

感谢您的帮助!

麦克

1 个答案:

答案 0 :(得分:1)

嗯,这似乎是多方面的。让我们看看我是否可以提供帮助。最后一件事......

不会自动创建表格。

我将在Spring的文档中查看最基本方法的这一部分:Initialize a database using Hibernate。例如, on(dom.byId("uploadForm"), "change", function (event) { var fileName = event.target.value.toLowerCase(); if (sniff("ie")) { //filename is full path in IE so extract the file name var arr = fileName.split("\\"); fileName = arr[arr.length - 1]; } if (fileName.indexOf(".zip") !== -1) {//is file a zip - if not notify user generateFeatureCollection(fileName); } else { dom.byId('upload-status').innerHTML = '<p style="color:red">Add shapefile as .zip file</p>'; } }); function generateFeatureCollection (fileName) { var name = fileName.split("."); //Chrome and IE add c:\fakepath to the value - we need to remove it //See this link for more info: http://davidwalsh.name/fakepath name = name[0].replace("c:\\fakepath\\", ""); dom.byId('upload-status').innerHTML = '<b>Loading… </b>' + name; //Define the input params for generate see the rest doc for details //http://www.arcgis.com/apidocs/rest/index.html?generate.html var params = { 'name': name, 'targetSR': map.spatialReference, 'maxRecordCount': 1000, 'enforceInputFileSizeLimit': true, 'enforceOutputJsonSizeLimit': true }; //generalize features for display Here we generalize at 1:40,000 which is approx 10 meters //This should work well when using web mercator. var extent = scaleUtils.getExtentForScale(map, 40000); var resolution = extent.getWidth() / map.width; params.generalize = true; params.maxAllowableOffset = resolution; params.reducePrecision = true; params.numberOfDigitsAfterDecimal = 0; var myContent = { 'filetype': 'shapefile', 'publishParameters': JSON.stringify(params), 'f': 'json', 'callback.html': 'textarea' }; //use the rest generate operation to generate a feature collection from the zipped shapefile request({ url: portalUrl + '/sharing/rest/content/features/generate', content: myContent, form: dom.byId('uploadForm'), handleAs: 'json', load: lang.hitch(this, function (response) { if (response.error) { errorHandler(response.error); return; } var layerName = response.featureCollection.layers[0].layerDefinition.name; dom.byId('upload-status').innerHTML = '<b>Loaded: </b>' + layerName; addShapefileToMap(response.featureCollection); }), error: lang.hitch(this, errorHandler) }); } 将在每次运行应用程序时删除并重新创建表。初始开发工作简单易行。更强大的是利用像Flyway or Liquibase这样的东西。

序列化问题

因此,如果没有日志,并且没有创建表,那么缺少持久层将是假定的罪魁祸首。也就是说,当您拥有表和数据时,如果您没有所有相关表的存储库,则最终会出现StackOverflow错误(序列化变为循环)。为此,您可以使用@JsonBackReference(孩子)和@JsonManagedReference(父母)。我成功地只为孩子使用spring.jpa.hibernate.ddl-auto: create-drop

产品[]

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

  1. @JsonBackReference更改为private Items[] items;。用@ElementCollection注释。

  2. 使用@Embeddable注释Item.class。