0

Hallo I have a problem with a many-to-many mapping in Hibernate.

Tables of the many-to-many relationship

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
EvilKarter
  • 267
  • 7
  • 22
  • You have `@OneToMany` on both sides of the relationship. Surely you need `@ManyToMany`? – Tom Anderson Jan 30 '13 at 10:03
  • @ TOm Anderson Hi, i build it from ManyToMany to OneToMany. I thought it would help. But I have the same problem like before. – EvilKarter Jan 30 '13 at 10:27
  • Both sides should be `@ManyToMany`. One should declare the details of the mapping (`@JoinTable` and so on), and the other should use `mappedBy` to make itself the inverse of the first. – Tom Anderson Jan 30 '13 at 14:27

1 Answers1

0

we found the solution of the Problem.

The problem was, that there were several attributes with joins. And one of this joins doesn't work because the join table was empty. We write a tool which fills this table with default values and now it works on a magic way.

We don't know how exactly this the problem solved but now it is gone and everything works. Probably one "nullable" statment was/is missing.

AND we all hate Hibernate ;-)

But one problem left. It is not possible to remove all entities from the add list. But this is an other problem ^^.

// 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);
    }

Thank you very much for your help!

EvilKarter
  • 267
  • 7
  • 22