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

Joined: Sun Jul 18, 2010 9:17 am
Posts: 16
Hi everyone,
I have to JPA Hibernate a 1-n relationship to the databank.
The relationship consists of a "user ", the number of "addresses" has.
Here are the code:
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;
...
}


And the model looks like this:
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;
   }


The address data (city, street, postal code) and the ID of the user must be stored in the User Address table.
Now I do not know why the model code works.
Does anyone here have a tip?
Thank you in advance for your help.


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.