From a7be9bebf28dfd5e11b13a269738a726b82ddf2b Mon Sep 17 00:00:00 2001 From: Kavoos Bojnourdi Date: Sat, 15 Aug 2015 16:54:45 +0200 Subject: [PATCH] Work on Profile Fix SQL (again) Code cleanup First version of tests for ProfilesService --- .../edf/gde/dao/impl/AttributeDaoImpl.java | 5 +++++ .../com/edf/gde/dao/impl/ProfileDaoImpl.java | 5 +++++ .../com/edf/gde/entities/AttributeGroup.java | 11 +---------- .../java/com/edf/gde/entities/Profile.java | 7 +------ .../com/edf/gde/test/dao/ProfileDaoTest.java | 2 +- projects/GDE_App/src/GDE_DB_Init.sql | 19 ++----------------- 6 files changed, 15 insertions(+), 34 deletions(-) diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java index a0167eb..52b7d3a 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java @@ -86,6 +86,11 @@ public class AttributeDaoImpl implements AttributeDao { public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); em.persist(group); + if (group.getAttributes() != null) { + for (Attribute a : group.getAttributes()) { + a.setAttributeGroup(group); + } + } return group.toAttributeGroupTO(); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java index f32d3c5..27790fb 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java @@ -26,6 +26,11 @@ public class ProfileDaoImpl implements ProfileDao { public ProfileTO createProfile(ProfileTO pto) { Profile profile = Profile.fromProtileTO(em, pto); em.persist(profile); + if (profile.getProfileAttributeCollection() != null) { + for (ProfileAttribute a : profile.getProfileAttributeCollection()) { + a.setProfile(profile); + } + } return profile.toProfileTO(); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java index e0b4e6f..08c7980 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java @@ -8,7 +8,6 @@ import com.edf.gde.transferables.AttributeTO; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; -import java.util.Objects; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -18,8 +17,6 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; @@ -50,13 +47,7 @@ public class AttributeGroup implements Serializable { @SequenceGenerator(name = "SEQ_GEN_SEQUENCE", allocationSize = 50) @Column(name = "id") private long id; - @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = {CascadeType.ALL}) - @JoinTable(name = "attribute_group_attribute", - joinColumns = { - @JoinColumn(name = "attributegroup_id", referencedColumnName = "id")}, - inverseJoinColumns = { - @JoinColumn(name = "attribute_id", referencedColumnName = "id", unique = true)}) - + @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = {CascadeType.ALL}, mappedBy = "attributeGroup") private Collection attributeCollection; public AttributeGroup() { diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java index b25e824..c52970f 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java @@ -57,12 +57,7 @@ public class Profile implements Serializable { @Size(max = 255) @Column(name = "name") private String name; - @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL) - @JoinTable(name = "PROFILE_ATTRIBUTE_PROFILE", - joinColumns = { - @JoinColumn(name = "profile_id", referencedColumnName = "id")}, - inverseJoinColumns = { - @JoinColumn(name = "profile_attribute_id", referencedColumnName = "id", unique = true)}) + @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "profile") private Collection profileAttributeCollection; public Profile() { diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/ProfileDaoTest.java b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/ProfileDaoTest.java index fa53ca1..69090ff 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/ProfileDaoTest.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/ProfileDaoTest.java @@ -101,7 +101,7 @@ public class ProfileDaoTest extends BaseTest { ProfileAttributeTO attributeTO = new ProfileAttributeTO(); attributeTO.setMandatory(true); attributeTO.setName("attribute1"); - //attributeTO.setProfileId(profileTO.getId()); + attributeTO.setProfileId(newProfileTO.getId()); attributeTO.setType("string"); List attributeTOs = new ArrayList<>(); attributeTOs.add(attributeTO); diff --git a/projects/GDE_App/src/GDE_DB_Init.sql b/projects/GDE_App/src/GDE_DB_Init.sql index 1a00db1..870b5f2 100644 --- a/projects/GDE_App/src/GDE_DB_Init.sql +++ b/projects/GDE_App/src/GDE_DB_Init.sql @@ -49,18 +49,10 @@ CREATE TABLE attribute ( name varchar(255), type varchar(255), value varchar(255), - attribute_group_id bigint REFERENCES attribute_group (id), + attribute_group_id bigint not null REFERENCES attribute_group (id), mandatory boolean ); -DROP TABLE IF EXISTS attribute_group_attribute CASCADE; -CREATE TABLE attribute_group_attribute -( - attributegroup_id bigint REFERENCES attribute_group (id) ON UPDATE NO ACTION ON DELETE NO ACTION, - attribute_id bigint REFERENCES attribute (id) ON UPDATE NO ACTION ON DELETE NO ACTION -); -alter table attribute_group_attribute add primary key (attributegroup_id, attribute_id); - /* PROFILE */ DROP TABLE IF EXISTS profile CASCADE; @@ -75,15 +67,8 @@ CREATE TABLE profile_attribute ( name varchar(255), type varchar(255), mandatory boolean, - profile_id bigint REFERENCES profile (id) -); - -DROP TABLE IF EXISTS PROFILE_ATTRIBUTE_PROFILE CASCADE; -CREATE TABLE PROFILE_ATTRIBUTE_PROFILE ( - profile_id bigint references profile(id) on update no action on delete no action, - profile_attribute_id bigint references profile_attribute on update no action on delete no action + profile_id bigint not null REFERENCES profile (id) ); -alter table PROFILE_ATTRIBUTE_PROFILE add primary key (profile_id, profile_attribute_id); /* DATA */ -- 2.39.2