From 05ab8cc02cfc6778b1bf4dfb16eaa6afadfffdea Mon Sep 17 00:00:00 2001 From: Kavoos Bojnourdi Date: Sun, 16 Aug 2015 09:45:42 +0200 Subject: [PATCH] Code cleanup Optimizing DAO collections during updates Fix potential problems when updating data --- .../java/com/edf/gde/dao/impl/AttributeDaoImpl.java | 6 +++++- .../src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java | 9 +++++++-- .../src/java/com/edf/gde/entities/Attribute.java | 5 +---- .../src/java/com/edf/gde/entities/AttributeGroup.java | 2 +- .../GDE-ejb/src/java/com/edf/gde/entities/Profile.java | 10 +++------- .../java/com/edf/gde/entities/ProfileAttribute.java | 2 +- 6 files changed, 18 insertions(+), 16 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 52b7d3a..7fcb879 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 @@ -104,10 +104,14 @@ public class AttributeDaoImpl implements AttributeDao { @Override public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { + if (agto.getAttributeCollection() != null) { + for (AttributeTO attributeTO : agto.getAttributeCollection()) { + attributeTO.setGroupId(agto.getId()); + } + } AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); AttributeGroup up = em.merge(group); Collection attributeCollection = agto.getAttributeCollection(); - return up.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 6bc9d59..7fdf553 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 @@ -24,7 +24,7 @@ public class ProfileDaoImpl implements ProfileDao { @Override public ProfileTO createProfile(ProfileTO pto) { - Profile profile = Profile.fromProtileTO(em, pto); + Profile profile = Profile.fromProfileTO(em, pto); em.persist(profile); if (profile.getProfileAttributeCollection() != null) { for (ProfileAttribute a : profile.getProfileAttributeCollection()) { @@ -48,7 +48,12 @@ public class ProfileDaoImpl implements ProfileDao { @Override public ProfileTO updateProfile(ProfileTO profileTO) { - Profile profile = Profile.fromProtileTO(em, profileTO); + if (profileTO.getAttributes() != null) { + for (ProfileAttributeTO attributeTO : profileTO.getAttributes()) { + attributeTO.setProfileId(profileTO.getId()); + } + } + Profile profile = Profile.fromProfileTO(em, profileTO); em.merge(profile); return profile.toProfileTO(); } 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 27325b8..68523f5 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 @@ -7,7 +7,6 @@ import com.edf.gde.transferables.AttributeTO; import java.io.Serializable; import java.util.Objects; import javax.persistence.Basic; -import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityManager; @@ -126,9 +125,7 @@ public class Attribute implements Serializable { attribute.type = ato.getType(); attribute.value = ato.getValue(); attribute.mandatory = ato.getMandatory(); - if (ato.getGroupId() != 0) { - attribute.attributeGroup = em.find(AttributeGroup.class, ato.getGroupId()); - } + attribute.attributeGroup=ato.getGroupId()==0?null:em.find(AttributeGroup.class, ato.getGroupId()); return attribute; } 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 08c7980..1228fdd 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 @@ -47,7 +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}, mappedBy = "attributeGroup") + @OneToMany(fetch = FetchType.LAZY, 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 c52970f..163a34e 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 @@ -3,7 +3,6 @@ */ package com.edf.gde.entities; -import com.edf.gde.transferables.AttributeTO; import com.edf.gde.transferables.ProfileAttributeTO; import com.edf.gde.transferables.ProfileTO; import java.io.Serializable; @@ -20,8 +19,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; @@ -30,7 +27,6 @@ import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; /** * @@ -57,7 +53,7 @@ public class Profile implements Serializable { @Size(max = 255) @Column(name = "name") private String name; - @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "profile") + @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "profile") private Collection profileAttributeCollection; public Profile() { @@ -90,7 +86,7 @@ public class Profile implements Serializable { public void setProfileAttributeCollection(Collection profileAttributeCollection) { this.profileAttributeCollection = profileAttributeCollection; } - + public void addProfileAttribute(ProfileAttribute attribute) { if (profileAttributeCollection == null) { setProfileAttributeCollection(new ArrayList()); @@ -98,7 +94,7 @@ public class Profile implements Serializable { profileAttributeCollection.add(attribute); } - public static Profile fromProtileTO(EntityManager em, ProfileTO profileTO) { + public static Profile fromProfileTO(EntityManager em, ProfileTO profileTO) { Profile profile = new Profile(); profile.setId(profileTO.getId()); profile.setName(profileTO.getName()); diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java index dd3a191..2ff80f9 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java @@ -110,7 +110,7 @@ public class ProfileAttribute implements Serializable { attribute.mandatory = attributeTO.isMandatory(); attribute.name = attributeTO.getName(); attribute.type = attributeTO.getType(); - attribute.profile = em.find(Profile.class, attributeTO.getProfileId()); + attribute.profile = attributeTO.getProfileId()==0? null:em.find(Profile.class, attributeTO.getProfileId()); return attribute; } -- 2.39.2