-->
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.  [ 2 posts ] 
Author Message
 Post subject: A -> B Beziehung / persist()
PostPosted: Tue Sep 14, 2010 4:35 pm 
Newbie

Joined: Thu Apr 08, 2010 8:47 am
Posts: 18
Hi,

ich habe ein kleines Verständnis-Problem:
Wenn ich eine Entität A habe, die eine Entität B besitzt:
Code:
@Test
    public void testAddAB() {
        A a = new A();
        a.setA("a");
        B b = new B();
        b.setB("b");
        a.setB(b);
        //ObjectController.create(b);  <<<
        ObjectController.create(a);
    }

Das Objekt a wird nur in der DB erzeugt, wenn ich das Kommentar (<<<) entferne.
Die Methode create sieht folgendermaßen aus:
Code:
   public static void create(Object obj) {
      EntityTransaction tx = null;
      try {
         tx = EntityManagerUtil.em.getTransaction();
         tx.begin();
         EntityManagerUtil.em.persist(obj);
         tx.commit();
      } catch (RuntimeException e) {
         if (tx != null && tx.isActive()) {
            try {
               tx.rollback();
            } catch (HibernateException e1) {
               System.out.println("HibernateException");
            }
            throw e;
         }
      }
   }


Wieso ist das so? Im Buch "JPA" mit Hibernate habe ich gelesen, dass B automatisch erzeugt wird?

Vielen Dank!


Top
 Profile  
 
 Post subject: Re: A -> B Beziehung / persist()
PostPosted: Tue Sep 14, 2010 4:55 pm 
Newbie

Joined: Thu Apr 08, 2010 8:47 am
Posts: 18
Habe das Problem gelöst in der Klasse A beim "@Kardinalität"
@OneToOne(cascade=CascadeType.ALL)
Neben ALL beinhaltet:
MERGE,PERSIST,REFRESH,REMOVE
Am besten mal anschauen!

Close!


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

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.