-->
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] Propagation de la persistance
PostPosted: Wed Oct 21, 2009 9:58 am 
Newbie

Joined: Wed Oct 21, 2009 9:18 am
Posts: 2
Bonjour,

je débute avec hibernate et je n'arrive pas à propager de la persistance entre 2 simples classes malgré la lecture de plusieurs tutoriaux (qui a dit que j'étais un boulet ? ;-)).

Voici le code en question :

User :
Code:
@Entity
@Table(name="user_t")
public class User implements Serializable {
   @Id
   @Column(name="id")
   @GeneratedValue(strategy=GenerationType.AUTO)   
   private long id;

   @OneToMany(mappedBy="user",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
   private List<CatalogAccount> catalogAccounts;
   
   public User() {
      id = 0L;
      catalogAccounts = new ArrayList<CatalogAccount>();
   }
   
   public List<CatalogAccount> getCatalogAccounts() {
      return this.catalogAccounts;
   }

   public void setCatalogAccounts(List<CatalogAccount> pCatalogAccounts) {
      this.catalogAccounts = pCatalogAccounts;
   }
   
   public void addCatalogAccount(CatalogAccount catalogAccount) {
      if ((catalogAccount != null) && (!this.catalogAccounts.contains(catalogAccount))) {
         this.catalogAccounts.add(catalogAccount);
      }
   }
}


CatalogAccount :
Code:
@Entity
@Table(name="catalogaccount_t")
@Inheritance(strategy=InheritanceType.JOINED)
public class CatalogAccount implements Serializable {
   
   @Id
   @Column(name="id")
   @GeneratedValue(strategy=GenerationType.AUTO)   
   private long id;
   
   @ManyToOne
   @JoinColumn(name="id",nullable=false)
   private User user;
   
   public CatalogAccount() {
      id = 0L;
      this.setUser(null);
   }

   public long getId() {
      return id;
   }

   public void setId(long pId) {
      id = pId;
   }
   
   public User getUser() {
      return user;
   }

   public void setUser(User user) {
      if ((this.user != null) && (this.user != user)){
         this.user.removeCatalogAccount(this);
      }
      this.user = user;
      if (user != null) {
         user.addCatalogAccount(this);
      }
   }
}


Code de test :
Code:
emf = Persistence.createEntityManagerFactory("sample");
em = emf.createEntityManager();
User lUser = new User();
CatalogAccount lAccount = new CatalogAccount();
lUser.addCatalogAccount(lAccount);
System.out.println("<<<<<<<<<<<<<<<<<< Trace");
em.getTransaction().begin();
em.persist(lUser);
em.getTransaction().commit();
System.out.println(">>>>>>>>>>>>>>>>>>");


Et le résultat d'exécution :
Code:
<<<<<<<<<<<<<<<<<< Trace
Hibernate: insert into user_t (login, password) values (?, ?)
>>>>>>>>>>>>>>>>>>


Comme on peut le constater dans l'exécution, l'enregistrement en base n'est pas propagé à CatalogAccount... :-(

D'avance merci pour votre aide.


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.