0

Not sure why I have an issue here, but when I save with a CrudRepository with these objects, I get the SerializationException (with no further information). Can someone take a look at my objects and offer me some insight into why they can't serialize? My pom.xml is attached last as well in case that helps somehow. I'm using a Postgres database.

EDIT: The database and now - tables are created, but objects are not creating rows.

The actual CrudRepository interface:

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

ZanyDishAccount entity:

@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 + "]";
    }

}

Company entity:

@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+"]";
    }
}

Order entity:

@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+"]";
    }
}

Thanks for your help!

Mike

1 Answer 1

1

Hm, this seems multi-faceted. Let's see if I can help at all. Last thing first...

No tables being created automatically.

I would take a look at this section in Spring's docs for the most basic approach: 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.

Serialization issue

So without logs, and the fact that you have no tables created, the lack of a persistence layer would be the assumed culprit. That said, when you have tables and data, if you do not have a repository for all of the related tables, you'll end up with a StackOverflow error (the serialization becomes circular). For that, you can use @JsonBackReference (child) and @JsonManagedReference (parent). I have been successful using only @JsonBackReference for the child.

Items[]

I'm not sure what Item.class looks like, but that looks like an offensive configuration that I missed the first round.

  1. Change private Items[] items; to private List<Item> items = new ArrayList<Item>();. Annotate with @ElementCollection.

  2. Annotate Item.class with @Embeddable.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Brian, I was missing some basic stuff like the empty constructor and the constructor with each of the fields. My database is getting created now, but I'm getting this error: "org.springframework.orm.jpa.JpaSystemException: could not serialize". This is after I see a few "select nextval ('hibernate_sequence') rows output in the console window. No indication of any errors though. I have no idea why my objects can't serialize. Is there any way to get more debugging? hibernate.SQL and hibernate.type.descriptor.sql.BasicBinder are both set to TRACE. I'll update classes in the question. thx
I should have said, my database and tables are being created now. Just objects are not creating rows.
What if you run with --debug, and in application.properties set spring.jpa.show-sql=true? Do you have a full stacktrace? Also, I updated my answer as I noticed something additional (Items[]).
Brian- I can't thank you enough! Thanks so much - I really needed that- you're awesome. Thank you!!!! :) Cheers!
:) No problem at all, wish I caught that the first time. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.