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