【问题标题】:Hibernate Many-to-Many mapping: Add entries doesn't workHibernate 多对多映射:添加条目不起作用
【发布时间】:2013-01-30 09:24:48
【问题描述】:

您好,我在 Hibernate 中遇到了多对多映射的问题。

上面显示的表是通过多对多映射连接的。表 tbl_cp_group_relation 是具有 n:m 个连接的表。

在我的实体中,我通过多种方式解决了这个问题。我使用了多对多映射和多对一映射。在这两种方式中,它都起到了部分作用。我得到了一个充电点的组,我得到了一个组的充电点。 但我永远无法将充电点添加到一个或多个充电点。 如果我将充电点添加到程序运行的组中,并且组中还有更多充电点,直到我离开该功能。 如果我尝试将组添加到充电点,我总是会收到重复键错误消息。

这里是我的组实体。

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

这是我的充电点实体。

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

这里是我的函数代码

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

这里是cpGroupDAO的函数

public void updateChargingPoints(CpGroupEntity group) {
    entityManager.merge(group);
    entityManager.flush();
}

我不知道我的错误在哪里。但是,当我得到实体时,这不可能是错的。我只能在群组或充电点列表中删除或添加条目。

我希望有人可以帮助我。

【问题讨论】:

  • 您在关系的双方都有@OneToMany。你确定需要@ManyToMany
  • @TOm Anderson 嗨,我将它从多对多构建到单对多。我认为这会有所帮助。但是我遇到了和以前一样的问题。
  • 双方都应该是@ManyToMany。一个应该声明映射的详细信息(@JoinTable 等),另一个应该使用mappedBy 使自己成为第一个的倒数。

标签: java hibernate many-to-many transactional


【解决方案1】:

我们找到了问题的解决方案。

问题是,连接有几个属性。并且其中一个连接不起作用,因为连接表是空的。我们编写了一个工具,用默认值填充这个表,现在它以一种神奇的方式工作。

我们不知道这个问题究竟是如何解决的,但现在它已经消失了,一切正常。可能缺少一个“可为空”的语句。

我们都讨厌 Hibernate ;-)

但是还有一个问题。无法从添加列表中删除所有实体。但这是另一个问题^^。

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

非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多