0

Class1:

int field2
Class2 field1

Class2:

Class3 field3

Class3:

String field4
String field5

Class1 domain class:

@Table(name = "class1_details")
@Entity
public class Class1Details {
  @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long id;
    
    @Column(name = "class2_fields")
    private Class2 fields;
  
     // respective getters and setters

}

I am using springboot. I am extending JPARepository for my repo interface. I want to save class1 in db. I am getting below exception:

org.springframework.orm.jpa.JpaSystemException: could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize ...

Caused by: org.hibernate.type.SerializationException: could not serialize ....

Caused by: java.io.NotSerializableException: com.model.Class3 ...

Tried @ElementCollection but of no use. Please help with this.

2
  • your entity class should implement Serializable Commented Aug 13, 2018 at 12:42
  • Got it. Now another exception.. Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "class3_id" violates not-null constraint I havent declared any class3_id in class3. Do i need to create domain classes (@entity class) for class2 and class3 as well? Commented Aug 13, 2018 at 12:56

1 Answer 1

0

You should make nested classes @Embeddable:

@Entity
@Table(name = "my_entities")
public class MyEntity {
    //...

    private MyData data;
}

@Embeddable
public class MyData {
    private String value;
}

Then Hibernate will deal with the following table:

create table my_entities (
  -- MyEntity stuff
  --
  value varchar(255)
);

Another an interesting approach is storing nested class in DB as JSON, see my related answer...

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

Comments

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.