-->
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 bei einer 1-n Beziehung mit Set
PostPosted: Mon Jan 31, 2011 6:23 pm 
Newbie

Joined: Sun Jul 18, 2010 9:17 am
Posts: 16
Hi allerseits,
ich arbeite gerade in einem Projekt und muss eine OneToMany bidirectional-Beziehung mit JPA Hibernate realisieren.
Ich habe also einen User, der mehrere Addresse haben kann und mehrere Addresse gehören zu einem User. Also eine 1(User) zu n(Useraddress).
Die User Addresse Daten und die ID des aktuellen Users müssen in der Tabelle "Useraddresses" gespeichert werden.
Ich habe folgendes Problem:
Wenn ich die Addresse Daten des Users speichere, wird in der Tabelle "Useraddresses" und in Tabelle "Users" eine neue Zeile erzeugt, die leeren Attributen beinhaltet. Das heißt, die Daten werden nicht gespeichert, sowie die richtige ID des aktuellen Users.
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", fetch=FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
   private Set<Useraddress> useraddresses = new HashSet<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
...
}


Das 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);
   }
...
}


Ich suche seit drei Tagen eine Lösung, aber umsonst.
kann Jemand bitte mir hier helfen?
Vielen Dank für eure Hilfe.


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.