From 628b2d28a2690ebc7816f9e9f197d3344cc01f85 Mon Sep 17 00:00:00 2001 From: Bojnourdi Date: Thu, 13 Aug 2015 17:58:00 +0200 Subject: [PATCH] Attribute DAO --- .../GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java | 8 ++++++++ .../GDE-ejb/src/java/com/edf/gde/entities/Attribute.java | 4 +++- .../test/com/edf/gde/test/dao/AttributeDaoTest.java | 1 - projects/GDE_App/src/GDE_DB_Init.sql | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java index eaf8e69..200fae1 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.persistence.EntityManager; +import javax.persistence.Query; /** * @@ -27,6 +28,13 @@ public class AttributeDao { public AttributeTO createAttribute(AttributeTO ato) { Attribute a = Attribute.fromAttributeTO(ato); em.persist(a); + em.flush(); + if (a.getAttributeGroupId() != 0) { + Query q = em.createNativeQuery("INSERT INTO attribute_group_attribute (attributegroup_id,attribute_id) values (?,?)"); + q.setParameter(1, a.getAttributeGroupId()); + q.setParameter(2, a.getId()); + q.executeUpdate(); + } return a.toAttributeTO(); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java index 2935df6..279fb31 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java @@ -12,6 +12,8 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedNativeQuery; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.SequenceGenerator; @@ -57,7 +59,7 @@ public class Attribute implements Serializable { private Boolean mandatory; @Column(name = "attribute_group_id") private long attributeGroupId; - + public Attribute() { } diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java index 48d825e..1469819 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java @@ -65,7 +65,6 @@ public class AttributeDaoTest extends BaseTest { Assert.fail("Error deleting attribute"); } try { - System.out.println("Deleting group " + resultAttributeGroup.getId()); daoClient.deleteAttributeGroup(resultAttributeGroup.getId()); } catch (Exception e) { Assert.fail("Error deleting attribute group"); diff --git a/projects/GDE_App/src/GDE_DB_Init.sql b/projects/GDE_App/src/GDE_DB_Init.sql index 369f5a9..10ecb16 100644 --- a/projects/GDE_App/src/GDE_DB_Init.sql +++ b/projects/GDE_App/src/GDE_DB_Init.sql @@ -53,8 +53,8 @@ CREATE TABLE attribute ( DROP TABLE IF EXISTS attribute_group_attribute CASCADE; CREATE TABLE attribute_group_attribute ( - attributegroup_id bigint REFERENCES attribute_group (id), - attribute_id bigint REFERENCES attribute(id) + attributegroup_id bigint REFERENCES attribute_group (id) on delete cascade, + attribute_id bigint REFERENCES attribute(id) on delete cascade ); CREATE INDEX attribute_group_attribute_idx1 on attribute_group_attribute(attributegroup_id); CREATE INDEX attribute_group_attribute_idx2 on attribute_group_attribute(attribute_id); -- 2.39.2