Hello,
I've got the following issue: I need to have a many to many association with an additional attribute in the association table. I tried several methods, which I had found in the internet, but none of them worked correct for me. I now decided to use two One-To-Many associations instead. However I now have one last issue to solve:
First, here is my source (sample, reduced code):
Dokument.javaCode:
@Entity
public class Dokument {
@Id
@GeneratedValue
private Long id;
private String text;
@OneToMany(mappedBy="dokument", cascade = {CascadeType.ALL})
private List<DokumentAutor> dokumentAutoren = new ArrayList<DokumentAutor>();
/... default setters and getters .../
public void addAutor(Autor autor, String kommentar)
{
DokumentAutor da = new DokumentAutor(this, autor);
da.setKommentar(kommentar);
this.dokumentAutoren.add(da);
}
}
Autor.javaCode:
@Entity
public class Autor {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy="autor", cascade = {CascadeType.ALL})
private List<DokumentAutor> dokumente = new ArrayList<DokumentAutor>();
/... default setters and getters .../
public void addDokument(Dokument dokument, String kommentar)
{
DokumentAutor da = new DokumentAutor(dokument, this);
da.setKommentar(kommentar);
this.dokumente.add(da);
}
DokumentAutor.javaCode:
@Entity
public class DokumentAutor {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Dokument dokument;
@ManyToOne
private Autor autor;
private String kommentar;
protected DokumentAutor(){};
public DokumentAutor(Dokument dokument, Autor autor)
{
this.dokument = dokument;
this.autor = autor;
}
/... default setters and getters .../
}
Main.javaCode:
EntityManager em = HibernateUtil.getEntityManager();
em.getTransaction().begin();
Dokument mDokument = new Dokument();
Autor mAutor = new Autor();
mDokument.setText("Book");
mAutor.setName("Miller");
mDokument.addAutor(mAutor, "Mainauthor");
em.persist(mAutor);
em.persist(mDokument);
em.getTransaction().commit();
em.close();
At this point, there is an entry of each entity "Dokument", "Autor" and "DokumentAutor" in my database. If I now call anywhere after the persist
Code:
System.out.println(mAutor.getDokumente().toString());
I get an emtpy list, because my Java application has not read the new relation information from the database.
Also if I call
Code:
Autor rAutor = em.find(Autor.class, new Long(1));
System.out.println(rAutor.getDokumente().toString());
within the same transaction, I get an empty list, because the original object seems to be cached.
But If I close my first transaction and get a new one, my desired object is in the list.
Code:
em = HibernateUtil.getEntityManager();
em.getTransaction().begin();
Autor rAutor = em.find(Autor.class, new Long(1));
System.out.println(rAutor.getDokumente().toString());
em.close();
There must be an other way to do this without closing and getting a new transaction. I thought of mapping one side as @transient, but this doesn't seem to be the best solution, because it always requires both sides to be set.
I know, that this is probably not the best solution for a many-to-many association with additional attributes, but there is just a small bit missing until it works.
Thanks in advance for your help!
Best wishes,
Stefan