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

741 views Asked by At

i am witnessing weird behavior in hibernate , i have many to many relation and i have tables as shown below

Attribute -- Attribute_Category -- Category

when i try running my DAO layer test , hibernate inserting rows in all three tables including join 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();
    }
}

but when i try do the same thing in service layer, only mapping tables getting inserted but not join tables.

my service implementation code

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

i dont see the difference between the dao test and service implementation, all i am mapping bo to do objects, and i do identifier merge as shown below

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

my category entity

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

need assistance to solve this weird problem

1

There are 1 answers

0
pappu_kutty On BEST ANSWER

1 .dao i am using same do objects for saving/merging operations, where as in service layer, i have to map bo to do objects and now as per hibernate rule, no two objects will have same identifier, but in my case both bo and do object will have same identifier, so i have to do merge objects if they have same identifier (category), after that i have to save attribute object which i am adding newly.

2 another way,get the object of that identifier

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

and do saveorupdate since we are using same object , no need to merge operation here, this is more relevant than previous one