I have the following problem: I have three connected classes. I have annotated them but I am getting wrong results (described below):
@Entityd
@Table(name = "ClassA")
public class ClassA{
@Id
@GeneratedValue
private Long id = 0L;
...
@OneToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@Column(name = "ClassBList")
private List<ClassB> listB;
...
}
@Entity
@Table(name="ClassB")
public class ClassB {
@Id
@GeneratedValue
private Long id = 0L;
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@Column(name = "ClassCList")
private List<ClassC> listC;
...
}
@Entity
@Table(name="ClassC")
public class ClassC {
@Id
@GeneratedValue()
private Long id = 0L;
...
@ElementCollection
private List<String> listD;
...
}
When I work with this structure for the first ClassA I create,save and load everything is ok. For a new instance of ClassA which I save to repo and load again, I suddenly have the strings of the first ClassA in listD.
The result I need is that every class is "independently" saved. So the collections of each class should hold unique (each one with its own id and sublists) objects.
What would be the best way (annotations) to model this classes in Java 8 with Spring Boot 2.2.0.M5 and javax.persistence-api 2.2 ?
EDIT:
I have now removed class B and rewrote classA to:
@Entity
@Table(name = "ClassA")
public class ClassA{
@Id
@GeneratedValue
private Long id = 0L;
...
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
@MapKey(name = "type")
private Map<String,Set<ClassC>> classCmap;
...
}
This is giving me an error like:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class
How can I model/solve/annotate this?
wrong resultdo you consider that you are having?listDthe strings are accumulating.{ "key":"value"but I would like to have this response{ "key": ["values"]