开发者

@ManyToMany association isn't inserted in the second table

I want to insert data into a table that is associated with another table in a relationship ManyToMany. When I insert the data, it is inserted into the table but the association with the other data that is in the second table is not. This is a Java EE application using JSF2+Spring+Hibernate.

Here is the entity:

     @Entity
     @Table(name="USER")
     public class User  {

private int id;
private String nom;
private Set<开发者_JS百科Formation> mesformations;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(int id) {
    this.id = id;
}

/**
 * @return the nom
 */
@Column(name="NOM",length=50)
public String getNOM() {
    return nom;
}
/**
 * @param nom the nom to set
 */
public void setNom(String nom) {
    this.nom = nom;
}
/**
 * @return the mesFormations
 */
@ManyToMany
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
     @JoinTable(name = "USER_FORM",
                       joinColumns = @JoinColumn(name = "user_id",
                               referencedColumnName = "USER_ID"),  
        inverseJoinColumns = @JoinColumn(name = "form_id", referencedColumnName = "ID"))

    public Set<Formation> getMesFormations() {
    return mesFormations;
}
/**
 * @param mesFormations the mesFormations to set
 */
public void setMesFormations(Set<Formation> mesFormations) {
    this.mesFormations = mesFormations;
}

public void addToFormation(Formation formation) {

    if(mesFormation==null)
    {
        mesFormations=new HashSet<Formation>();
    }

    mesFormations.add(formation);

}

.....

   }

Formation.java

 @Entity
 @Table(name="Foramtion")
 public class Formation {

private int id;
private String nomFormation;
private int nombreMatiere;

private Set<User> mesUsers;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(int id) {
    this.id = id;
}


/**
 * @return the mesUsers
 */
@ManyToMany(mappedBy = "mesFormations",fetch=FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
public Set<User> getMesUsers() {
    return mesUsers;
}

/**
 * @param mesUsers the mesUsers to set
 */
public void setMesUsers(Set<User> mesUsers) {
    this. mesUsers =  mesUsers;
}


/**
 * @return the nomFormation
 */
@Column(name="NOM_FORMATION",length=50,unique=true)
public String getNomFormation() {
    return nomForamtion;
}
/**
 * @param nomFormation the nomForamtion to set
 */
public void setNomForamtion(String nomForamtion) {
    this.nomForamtion = nomForamtion;
}

/**
 * @return the nombreMatiere
 */

public int getNombreMatiere() {
    return nombreMatiere;
}
/**
 * @param nombreMatiere the nombreMatiere to set
 */
public void setNombreMatiere(int nombreMatiere) {
    this.nombreMatiere = nombreMatiere;
}

public void addToUser(User user) {

   if(mesUser==null)
    {
      mesUsers=new HashSet<User>();

    }
       mesUsers.add(user);
       user.addToFormation(this); 


}

public void removeFromUser(User user) {
    this.getMesUsers().remove(user);
    user.getMesUsers().remove(this);
}


 }

the method of the DAO layer which allows for the persistence of a user

    public User enregistrer(User user) {
    // TODO Auto-generated method stub
      this.getSession().beginTransaction();
     this.getSession().persist(user);
     this.getSession().beginTransaction().commit();
     return Entity ;
  }

the method of the service layer that allows to call the save method of the dao layer

  public User persistUser(User user, List<Integer> idList){

    for(Integer id : idList){

        Formation form = iformationDao.findById(id);
                     form.addToUser(user);

    }
    return iuserDao.enregistrer(user);

thank for answering


It looks to me like you have your CascadeTypes set to:

@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})

yet you are calling:

this.getSession().persist(user);

I think you will need to add CascadeType.PERSIST to your @Cascade annotation to get the behavior you desire.


change from

public User enregistrer(User user) {
// TODO Auto-generated method stub
  this.getSession().beginTransaction();
 this.getSession().persist(user);
 this.getSession().beginTransaction().commit();
 return Entity ;

}

to

   public User enregistrer(User user) {
    // TODO Auto-generated method stub
     Transaction tx =  this.getSession().beginTransaction();//change
     this.getSession().persist(user);
     tx.commit();//change 
     return Entity ;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜