Reputation: 5
I have an entity with two Embedded
classes of the same type and which one has an ElementCollection
of the same type two. The business logic is apparently correct, but I am experiencing some problems with lack of knowledge in JPA, I guess.
Let's check my classes:
@Entity
public class Etapa extends EntidadeBase {
@Embedded
private CronogramaDeDesembolso cronogramaDeReceita;
@Embedded
private CronogramaDeDesembolso cronogramaDeDespesa;
}
@Embeddable
public class CronogramaDeDesembolso {
@ElementCollection
private List<Parcela> parcelas;
}
I am receiving the following error log.
Caused by: org.hibernate.HibernateException: Found shared references to a collection: nexxus.convenioestadual.dominio.planodetrabalho.etapa.Etapa.cronogramaDeReceita.parcelas
Do you guys have any clue of what is wrong and how can I fix it?
EDIT:
Due comments I did this edit and it do not worked too
@Entity
public class Etapa extends EntidadeBase {
@Embedded
@AttributeOverride(name = "parcelas", column = @Column(name = "parcelasReceita"))
private CronogramaDeDesembolso cronogramaDeReceita;
@Embedded
@AttributeOverride(name = "parcelas", column = @Column(name = "parcelasDespesa"))
private CronogramaDeDesembolso cronogramaDeDespesa;
}
Upvotes: 0
Views: 2270
Reputation: 31
Is there any reason why you have decided to use this structure ? Typically when converting an object to an RDBMS you would need to model the relationships. When you use an embeddable it will add the column (or columns) associated with it to the table. So when you do this normally (not collections) it is fine.
When you do a collection it runs into issues. Mainly there is no way to represent a collection in a single row (since this is an entity you could have many of them so effectively for each object you only have one row) & one column. So when you represent a collection you actually have to have a second table with a column referencing it back to the first. It's really the opposite thinking of a normal object. The collection entries need to know what collection they were associated with instead of the collection being knowledgeable of its entries.
So in some POJO you could have and these....
MyListObject {
//Some implementation of things you want to collect
}
MyClass {
List<MyListObject> myListObject;
}
But to model this in JPA you would need to have these represented by two tables.
Your object that will be in the list.
@Entity
MyListObject {
@ManyToOne
@JoinColumn(name = "MY_CLASS_KEY")
private MyClass myClass;
}
Your object/entity that will have the list.
@Entity
MyClass {
@Id
@Column(name = "MY_CLASS_KEY")
private Long myClassKey;
@OneToMany(mappedBy = "myClass")
private List<MyListObject> myString;
}
I hope this helps.
Upvotes: 1
Reputation: 1342
A quick search on Google turned up this in StackOverflow:
It would seem as though you have to do some explicit annotation overriding over the fields within the embeddable class. There are some code examples in the linked answer as well that should give you a good idea of where to go.
Cheers,
Upvotes: 0