休眠多对多映射不将行插入服务层的连接table

hibernate many to many mapping not inserting row into join table from service layer

我在休眠中目睹了奇怪的行为,我有多对多的关系,我有 tables 如下所示

Attribute -- Attribute_Category -- Category

当我尝试 运行 我的 DAO 层测试时,hibernate 在所有三个 table 中插入行,包括连接 table ATTRIBUTE_CATEGORY.

@Test
@Rollback(false)
public void testAddAttribute(){
    try {
        AttributeDO attributeDO = getAttributeDO();
        List<AttributeDO> attributeDOs = new ArrayList<AttributeDO>();
        CategoryDO categoryDO = getAttributeDO().getCategoryDOs().get(0);
        attributeDOs.add(attributeDO);
        categoryDO.setAttributeDOs(attributeDOs);
        attributeDAOImpl.addAttribute(attributeDO);
        System.out.println("attribute id - " + attributeDO.getAttributeId());
    } catch (DataException cExp) {
        cExp.printStackTrace();
        Assert.fail();
    }
}

但是当我尝试在服务层做同样的事情时,只有映射 tables 被插入但没有加入 tables.

我的服务实现代码

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addAttribute(AttributeBO attributeBO) throws CrafartServiceException {
    AttributeDO attributeDO = mapper.mapAttributeBOToDO(attributeBO, new AttributeDO());
    CategoryDO categoryDO = mapper.mapCategoryBOToDO(attributeBO.getCategoryBO(), new CategoryDO(), new SeoDO());
    List<CategoryDO> categoryDOs = new ArrayList<>();
    categoryDOs.add(categoryDO);
    attributeDO.setCategoryDOs(categoryDOs);
    // creating bi directional relation ship between attribute --> category table. (category --> attribute cause unidirectional relation)
    List<AttributeDO> attributeDOs = new ArrayList<>();
    attributeDOs.add(attributeDO);
    categoryDO.setAttributeDOs(attributeDOs);
    try {
        attributeDAOImpl.addAttribute(attributeDO);
        attributeBO.setAttributeId(attributeDO.getAttributeId());
    } catch (DataException cExp) {
        throw new ServiceException("Service error - error while adding attribute", cExp);
    }
}

我看不出 dao 测试和服务实现之间的区别,我只是映射 bo 来做对象,我做标识符合并,如下所示

@Entity
@Table(name = "ATTRIBUTE")
public class AttributeDO implements Cloneable, Serializable {

/**
 * serial id
 */
private static final long serialVersionUID = -8629832877426207073L;


@Id
@Column(name = "attribute_id")
@SequenceGenerator(name = "seq_attribute", sequenceName = "seq_attribute", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_attribute")
private long attributeId;


@ManyToMany(mappedBy = "attributeDOs", cascade=CascadeType.MERGE)
private List<CategoryDO> categoryDOs;

我的类别实体

@Entity
@Table(name = "CATEGORY")
public class CategoryDO implements Serializable, Cloneable {
/**
 * 
 */
private static final long serialVersionUID = -870423497459160593L;

@Id
@Column(name = "category_id")
@SequenceGenerator(name = "seq_category", sequenceName = "seq_category", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_category")
private long categoryId;

@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "ATTRIBUTE_CATEGORY", joinColumns = { @JoinColumn(name = "SUB_CATEGORY_ID") }, inverseJoinColumns = { @JoinColumn(name = "ATTRIBUTE_ID") })
private List<AttributeDO> attributeDOs;

需要帮助解决这个奇怪的问题

1 .dao 我对 saving/merging 操作使用相同的 do 对象,在服务层中,我必须将 bo 映射到 do 对象,现在根据休眠规则,没有两个对象具有相同的标识符,但在我的情况下,bo 和 do 对象都将具有相同的标识符,因此如果它们具有相同的标识符(类别),我必须合并对象,之后我必须保存我新添加的属性对象。

2另一种方式,得到那个标识符的对象

CategoryDO object = session.get(CateogryDO.class, categoryDO.getCategoryId);

并执行保存或更新,因为我们使用的是同一个对象,这里不需要合并操作,这比之前的更相关