[英]Save Nested POJO class Objects using hibernate
int field2
Class2 field1
Class3 field3
String field4
String field5
@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. 我正在使用springboot。 I am extending JPARepository for my repo interface. 我正在为我的repo接口扩展JPARepository。 I want to save class1 in db. 我想在数据库中保存class1。 I am getting below exception: 我得到以下异常:
org.springframework.orm.jpa.JpaSystemException: could not serialize; org.springframework.orm.jpa.JpaSystemException:无法序列化; nested exception is org.hibernate.type.SerializationException: could not serialize ... 嵌套的异常是org.hibernate.type.SerializationException:无法序列化...
Caused by: org.hibernate.type.SerializationException: could not serialize .... 由以下原因引起:org.hibernate.type.SerializationException:无法序列化...。
Caused by: java.io.NotSerializableException: com.model.Class3 ... 引起原因:java.io.NotSerializableException:com.model.Class3 ...
Tried @ElementCollection but of no use. 尝试了@ElementCollection但没有用。 Please help with this. 请帮忙。
You should make nested classes @Embeddable : 您应该使嵌套类@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: 然后,Hibernate将处理下表:
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 ... 另一个有趣的方法是将嵌套类作为JSON存储在数据库中,请参阅我的相关答案 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.