EntityManager em = ModelFactory.getEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
Attribute attribute = em.createQuery(
"select attribute from Attribute attribute where attribute.name = ?1", Attribute.class).setParameter(1, data.getKey1())
.getSingleResult();
// before removing attribute, unlink children
List<Attribute> children = em.createQuery("select attribute from Attribute attribute where attribute.parent.name = ?1", Attribute.class).
setParameter(1, data.getKey1()).getResultList();
for (Attribute child: children) {
child.setParent(attribute.getParent());
}
// delete association with directories
for (Directory dir: attribute.getDirectories()) {
dir.getAttributes().remove(attribute);
}
attribute.getDirectories().clear();
// delete association with images
for (Image img: attribute.getImages()) {
img.getAttributes().remove(attribute);
}
attribute.getImages().clear();
// delete attribute
em.remove(attribute);
tx.commit();
} catch (Exception ex) {