Hallo I have a problem with a many-to-many mapping in Hibernate.
The tables shown above, are connected with a many-to-many mapping. The table tbl_cp_group_relation is the table with the n:m connections.
In my entities I solved this problem on several ways. I used the many-to-many mapping and the many-to-one mapping. In both ways it worked partially. I got the groups of a charging point and I got the charging points of a group. BUT I never could add charging points to a group or groups to charging point. If I add charging points to a group the program runs through and I also have more charging points in the group, till I left the function. If I try to add a group to a charging point I always get a duplicate-key error message.
Here my group entity.
@Entity
@Table(name = "tbl_cp_group")
public class CpGroupEntity {
...
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "tbl_cp_group_relation", joinColumns = { @JoinColumn(name = "cp_group_id") }, inverseJoinColumns = { @JoinColumn(name = "cp_id") })
private List<ChargingPointEntity> cps = new ArrayList<ChargingPointEntity>();
...
}
Here my charging point entity.
@Entity
@Table(name = "tbl_charging_point")
public class ChargingPointEntity {
...
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "tbl_cp_group_relation", joinColumns = { @JoinColumn(name = "cp_id") }, inverseJoinColumns = { @JoinColumn(name = "cp_group_id") })
private List<CpGroupEntity> groups = new ArrayList<CpGroupEntity>();
...
}
Here the code of my functions
@Transactional
public void administrateGroupCP(GroupDiff diff, long groupId, String username) throws BadInputRoutingException, NoSuchGroupException,
NotAuthorizedException, NoSuchChargingPointException {
try {
// Create add list
List<Long> addList = diff.getAdd();
// Get entities from database
CpGroupEntity groupEntity = cpGroupDAO.getGroup(groupId);
UserEntity userEntity = userDAO.getUser(username);
// Add charging points
addChargingPoints(addList, groupEntity, userEntity);
// Remove charging points
// removeChargingPoints(removeList, groupEntity, userEntity);
} catch (EntityNotFoundException e) {
throw new NoSuchGroupException();
}
}
@Transactional
private void addChargingPoints(List<Long> addList, CpGroupEntity groupEntity, UserEntity userEntity) throws NoSuchChargingPointException,
BadInputRoutingException {
...
// Add charging points to group
// 1. Clear list of charging points. Remove charging points which are to ignore from the charging point list with the add items
addEntitiesList.removeAll(ignore);
if (0 < addEntitiesList.size()) {
// 2. Add charging points to charging point list of entity
groupEntity.getCps().addAll(addEntitiesList);
// 3. Save changes
cpGroupDAO.updateChargingPoints(groupEntity);
}
}
}
Here the function of the cpGroupDAO
public void updateChargingPoints(CpGroupEntity group) {
entityManager.merge(group);
entityManager.flush();
}
I don't know where my errors are. But so wrong can't it be, when I get the entities. I only can't remove or add entries to the lists of groups or charging points.
I hope someone can help me.