0

I have an entity like :

@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())
}
}

Here I'm getting the size of the list is "0" but I'm sure I have 1 user in the join table and In my controller I have this :

//Reunion
reunion.getUsers().add(user);
rs.saveReunion(reunion);
System.out.println(rs.getReunion(reunion.getId()).getUsers().size())

And here I'm getting "1"

Any help would be appreciated thanks.

EDIT

For Insertion I'm just implementing JpaRepositiry from spring data jpa :

DAO :

@Repository
public interface ReunionDAO extends JpaRepository<Reunion, Long> {}

Service :

@Service
@Transactional
public class ReunionServiceImpl implements ReunionService {

@Autowired
private ReunionDAO rDAO;

@Override
public void saveReunion(Reunion r) {

    rDAO.save(r);
}
}
OddDev
  • 1,521
  • 7
  • 23
  • 45

0 Answers0