我有一个像:
这样的实体@Entity
@EntityListeners(ReunionListener.class)
public class Reunion implements Serializable{
private Long id;
@ManyToMany(cascade = {CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.DETACH,
CascadeType.REFRESH },
fetch = FetchType.EAGER)
@JoinTable(name = "Reunion_User",
joinColumns = @JoinColumn(name = "Reunion_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "User_id", referencedColumnName = "id")
)
private Set<User> users = new HashSet<>();
//Getters & setters...
}
ReunionListener :
public class ReunionListener {
@PostPersist
public void reunionPostPersist(Reunion ob) {
System.out.println(ob.getUsers().size())
}
}
我在这里获得列表的大小是&#34; 0&#34;但我确定在联接表中有1个用户,在我的控制器中我有这个:
//Reunion
reunion.getUsers().add(user);
rs.saveReunion(reunion);
System.out.println(rs.getReunion(reunion.getId()).getUsers().size())
在这里,我得到&#34; 1&#34;
感谢任何帮助。
修改
对于插入,我只是从spring数据jpa实现JpaRepositiry:
DAO:
@Repository
public interface ReunionDAO extends JpaRepository<Reunion, Long> {}
服务:
@Service
@Transactional
public class ReunionServiceImpl implements ReunionService {
@Autowired
private ReunionDAO rDAO;
@Override
public void saveReunion(Reunion r) {
rDAO.save(r);
}
}