-->
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: Problem with a 1-n relationship with Set
PostPosted: Mon Jan 31, 2011 6:20 pm 
Newbie

Joined: Sun Jul 18, 2010 9:17 am
Posts: 16
Hi everyone,
I'm working on a project and realize must be a bidirectional OneToMany relationship with Hibernate JPA.
So I have a user which can have multiple address and a multiple address belonging to a user. So a 1 (user) to n (User Address).
The user address data and the ID of the current user must be stored in the table "user address".
I have the following problem:
If I save the address data of the user in the table "user address" and created in table "Users" a new line containing the empty attributes. That is, the data is not stored, and the correct ID of the current user.
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", fetch=FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
   private Set<Useraddress> useraddresses = new HashSet<Useraddress>();
...
public void addUseraddress(Useraddress useraddress) {
      useraddress.setUser(this);
      useraddresses.add(useraddress);
   }
...
// Setter and Getter
...
}


In Useraddress.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;

...

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


The Model:
Code:
...
@Service("userService")
@Repository
public class UserServiceImpl implements UserService {
...
private EntityManager em;
@PersistenceContext
    public void setEntityManager(EntityManager em) { this.em = em; }
   public EntityManager getEntityManager() { return em; }
   @Transactional(readOnly = true)
   public void persistUseraddress(Useraddress useraddress) {
      // TODO Auto-generated method stub
      useraddress = new Useraddress();
      User user = new User();
      user.addUseraddress(useraddress);
      em.persist(useraddress);
   }
...
}


Seeking a solution for three days, but in vain.
Can someone please help me here?
Thank you 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.