通过阅读文档,我试图在Eventi和Followers之间建立双向的多对多关系,了解到如果我不保存Eventi和Followers实体的两个对象,则不会填充连接表...
在一个jsp页面中,我想在添加新的关注者时选择多个Eventi
我想要的是在创建新的关注者时选择多个Eventi,但是联接表为空。抱歉,但是我对Hibernate和Spring MVC完全陌生
Eventi模型
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "descrizione")
private String descrizione;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "ev_foll", joinColumns = {
@JoinColumn(name = "idEvento", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "idFollower", referencedColumnName = "id")
})
private Collection<Followers> followersCollection;
public void setFollowersCollection(Collection<Followers> followersCollection) {
this.followersCollection = followersCollection;
}
public Collection<Followers> getFollowersCollection() {
return followersCollection;
}
@JoinColumn(name = "idCat", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.EAGER)
private Categorie idCat;
public Eventi() {
}
public Eventi(int id, String descrizione, Collection<Followers> followersCollection, Categorie idCat) {
this.id = id;
this.descrizione = descrizione;
this.followersCollection = followersCollection;
this.idCat = idCat;
}
public int getId() {
return id;
}
public String getDescrizione() {
return descrizione;
}
public void setId(int id) {
this.id = id;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public Categorie getIdCat() {
return idCat;
}
public void setIdCat(Categorie idCat) {
this.idCat = idCat;
}
跟随模型
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@ManyToMany(mappedBy = "followersCollection", fetch = FetchType.LAZY, cascade = {CascadeType.ALL, CascadeType.PERSIST})
private Collection<Eventi> eventiCollection = new ArrayList<>();
public Followers() {
}
public Followers(int id, String email, String password, Collection<Eventi> eventiCollection) {
this.id = id;
this.email = email;
this.password = password;
this.eventiCollection = eventiCollection;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public Collection<Eventi> getEventiCollection() {
return eventiCollection;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setEventiCollection(Collection<Eventi> eventiCollection) {
this.eventiCollection = eventiCollection;
}
FollowersDaoImpl.java
@Autowired
private SessionFactory sessionFactory;
@Autowired
private Eventi eventi;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public Followers findById(int id) {
return (Followers) getSession().get(Followers.class, id);
}
@Transactional
@Override
public void saveFollowers(Followers followers)
getSession().persist(followers);
}
@Override
public void deleteFollowers(int id) {
Followers f = (Followers) getSession().load(Followers.class, id);
if (f != null) {
getSession().delete(f);
}
}
@SuppressWarnings("unchecked")
@Override
public List<Followers> findAllFollowers() {
Criteria criteria = getSession().createCriteria(Followers.class);
return (List<Followers>) criteria.list();
}
public List<Eventi> findAllEventi(){
Criteria criteria = getSession().createCriteria(Eventi.class);
return (List<Eventi>) criteria.list();
}
AppController
@RequestMapping(value = "/followers/add", method = RequestMethod.GET)
public String newFollowers(ModelMap model) {
Followers followers = new Followers();
List<Eventi> eventi = eventiService.findAllEventi();
model.addAttribute("tutti", eventi);
model.addAttribute("followers", followers);
this.followersService.saveFollowers(followers);
return "followers/add";
}
@RequestMapping(value = "/followers/add", method = RequestMethod.POST)
public String addFollowers(@ModelAttribute("followers") Followers f) {
if (f.getId() == 0) {
this.followersService.saveFollowers(f);
} else {
this.followersService.updateFollowers(f);
}
return "redirect:/followers/";
}