-->
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: JPA Hibernate: bidirectional Beziehung (OneToMany)
PostPosted: Thu Jan 13, 2011 7:04 pm 
Newbie

Joined: Sun Jul 18, 2010 9:17 am
Posts: 16
Hallo allerseits,
ich muss mit JPA Hibernate eine 1-n Beziehung in der Datenbank speichern.
Die Beziehung besteht aus einem "User", der mehrere "Adressen" hat.
Hier sind die Kode:
In user.java:
Code:
...
@Entity
@Table(name="Users")
@SequenceGenerator(name="user_gen", sequenceName="user_id_seq")
public class User implements Serializable {

        @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY, generator="user_gen")
   private int userID;
        //bi-directional many-to-one association to Useraddress
   @OneToMany(mappedBy="user", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
   private Set<Useraddress> useraddresses = new HashSet<Useraddress>();
       
        //Konstruktor

   public Set<Useraddress> getUseraddresses() {
      return this.useraddresses;
   }

   public void setUseraddresses(Set<Useraddress> useraddresses) {
      this.useraddresses = useraddresses;
   }
...
}


In useradress.java:
Code:
...
@Entity
@Table(name="UserAddresses")
@SequenceGenerator(name="useraddresses_gen", sequenceName="useraddresses_id_seq")
public class Useraddress implements Serializable {

        @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY, generator="useraddresses_gen")
   private int addressID;

         private String city;
         private String postcode;
         private String street;
         
        // Getter und Setter

        //bi-directional many-to-one association to User
        @ManyToOne(cascade = CascadeType.ALL)
   @JoinColumn(name="userID")
   private User user;
...
}


Und das Model sieht so aus:
Code:
...
/**
     * Saves address to person.
     */
   public void saveUseraddress(User user, Useraddress useraddress) {
      
      // UserId zuweisen
      int currentuserId = user.getUserID();
      
      User _user = new User(currentuserId);
      if (_user.getUseraddresses().contains(useraddress)) {
         _user.getUseraddresses().remove(useraddress);
        }

      _user.getUseraddresses().add(useraddress);
      em.persist(useraddress);
      
               return;
   }


Die Adressdaten (Stadt, Straße, postleitzahl) und die ID des Users müssen in der Useraddress-Tabelle gespeichert werden.
Nun weiß ich nicht, warum den Model-Code nicht funktioniert.
Hat jemand hier ein Tipp?
Danke im Voraus für eure Hilfe.


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: bidirectional Beziehung (OneToMany)
PostPosted: Tue Mar 01, 2011 9:38 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Eine Fehlerbeschreibung ist immer hilfreich. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


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.