我想将数据插入到与ManyToMany关系中的另一个表相关联的表中。插入数据时,它会插入到表中,但与第二个表中的其他数据的关联不会。这是一个使用JSF2 + Spring + Hibernate的Java EE应用程序。
这是实体:
@Entity
@Table(name="USER")
public class User {
private int id;
private String nom;
private Set<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);
}
}
允许用户持久化的DAO层的方法
public User enregistrer(User user) {
// TODO Auto-generated method stub
this.getSession().beginTransaction();
this.getSession().persist(user);
this.getSession().beginTransaction().commit();
return Entity ;
}
允许调用dao层的save方法的服务层的方法
public User persistUser(User user, List<Integer> idList){
for(Integer id : idList){
Formation form = iformationDao.findById(id);
form.addToUser(user);
}
return iuserDao.enregistrer(user);
感谢您的回答
答案 0 :(得分:0)
在我看来,您将CascadeTypes设置为:
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
然而你在呼唤:
this.getSession().persist(user);
我认为您需要在CascadeType.PERSIST
注释中添加@Cascade
才能获得所需的行为。
答案 1 :(得分:0)
从
改变public User enregistrer(User user) {
// TODO Auto-generated method stub
this.getSession().beginTransaction();
this.getSession().persist(user);
this.getSession().beginTransaction().commit();
return Entity ;
}
到
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 ;
}