0

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;
}
Dan
  • 10,303
  • 5
  • 36
  • 53

2 Answers2

1

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.

Andrew B.
  • 31
  • 4
0

A quick search on Google turned up this in StackOverflow:

JPA Multiple Embedded fields

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,

Community
  • 1
  • 1
Michael Platt
  • 1,297
  • 12
  • 25
  • I already tried that same post and did `@AttributeOverride(name = "parcelas", column = @Column(name = "parcelasDespesa"))` but it do not change the name of the attribute _parcelas_ and the error persists – higornucci Aug 18 '16 at 15:15
  • Did you use that line of code over each of the embedded objects? I believe they actually need to reference different physical columns in the database. So it would need to look something more like this (assuming I'm understand this correctly): `@Embedded` `@AttributeOverride(name = "parcelas", column = @Column(name = "parcelasDeReceita"))` `private CronogramaDeDesembolso cronogramaDeReceita;` `@Embedded` `@AttributeOverride(name = "parcelas", column = @Column(name = "parcelasDespesa"))` `private CronogramaDeDesembolso cronogramaDeDespesa;` – Michael Platt Aug 18 '16 at 15:17
  • I made the changes you suggested and it still do not worked =[ – higornucci Aug 18 '16 at 15:28
  • Hmm. Ok I will continue doing some research to see what I can find to help out. In the mean time, have you tried doing an embeddable class that only has strings and ints? I'm wondering if maybe the fact that you have a list of objects (also annotated or no?) is causing part of this problem. – Michael Platt Aug 18 '16 at 15:31
  • Thank you very much. I will try your suggestion and see where it leads me. – higornucci Aug 18 '16 at 17:09