-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-To-Many split in two One-To-Many - Incomplete DB result
PostPosted: Wed Aug 24, 2011 1:51 pm 
Newbie

Joined: Thu Mar 17, 2011 11:09 am
Posts: 8
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.java
Code:
@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.java
Code:
@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.java
Code:
@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.java
Code:
        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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.