-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Trouble saving multiple level cached items
PostPosted: Thu Sep 29, 2011 5:06 pm 
Newbie

Joined: Thu Sep 15, 2011 9:58 am
Posts: 4
I'm using annotations with a hibernate object that has multiple levels of children all dependent on parent's primary key (i.e. foreign key). Also, each child has an auto generated id as part of its primary key. I've been racking my brain for some time trying to get this to work when I save the top level object (i.e. entityManager.persist(parent)). Does anyone have an example that would be helpful. Attached is a snippet of an object that is a parent and child

Agent.java
Code:
@Configurable
@Entity
@Table(name="Agent")
public class Agent
{
   private AgentPK primaryKey;
   private List<Channel> channels = new ArrayList<Channel>();
   private MinorStageConfig minorStageConfig;
   public Agent()   {   }

   public void init(final MinorStageConfig minorStageConfig_, String id_, String name_, String type_)   {
      this.primaryKey = new AgentPK(minorStageConfig_);
      this.minorStageConfig = minorStageConfig_;   }

   @EmbeddedId
   public AgentPK getPrimaryKey()   {      return(this.primaryKey);   }

   public void setPrimaryKey(final AgentPK agentPK_)   {      this.primaryKey = agentPK_;   }

   @OneToMany(orphanRemoval=true, cascade=CascadeType.ALL,
              mappedBy="agent", fetch=FetchType.LAZY)
   public List<Channel> getChannels()
   {
      return(this.channels);
   }
   public void setChannels(final List<Channel> channels_)
   {
      this.channels = channels_;
   }
   @NotNull
   @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumns(
   {
      @JoinColumn(name="Minor_Stage_Configuration_ID_FK",
                  referencedColumnName="FDS_Minor_Stage_Configuration_ID",
                  insertable=false,
                  updatable=false),
      @JoinColumn(name="Minor_Stage_ID_FK",
                  referencedColumnName="Minor_Stage_ID_FK",
                  insertable=false,
                  updatable=false)
   })
   public MinorStageConfig getMinorStageConfig()
   {
      return(this.minorStageConfig);
   }

   public void setMinorStageConfig(final MinorStageConfig minorStageConfig_)
   {
      this.minorStageConfig = minorStageConfig_;
   }
}


AgentPK.java
Code:
@Configurable
@Embeddable
public class AgentPK implements Serializable
{
   private static final long serialVersionUID = 391264563586791285L;

   private Integer id;
   private Integer minorStageIdFK;
   private Integer minorStageConfigIdFK;
   private MinorStageConfig minorStageConfig;


   public AgentPK()
   {
   }


   public AgentPK(final MinorStageConfig minorStageConfig_)
   {
      this.minorStageConfig = minorStageConfig_;
   }

 
   @Column(name="FDS_Agent_ID")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public Integer getId()
   {
      return(this.id);
   }

   public void setId(final Integer id_)
   {
      this.id = id_;
   }

   @NotNull
   @Column(name="Minor_Stage_ID_FK")
   public Integer getMinorStageIdFK()
   {
      if(this.minorStageConfig != null)
      {
         this.minorStageIdFK = this.minorStageConfig.getPrimaryKey().getMinorStageIdFK();
      }

      return(this.minorStageIdFK);
   }

   public void setMinorStageIdFK(final Integer minorStageIdFK_)
   {
      this.minorStageIdFK = minorStageIdFK_;
   }


   @NotNull
   @Column(name="Minor_Stage_Configuration_ID_FK")
   public Integer getMinorStageConfigIdFK()
   {
      if(this.minorStageConfig != null)
      {
         this.minorStageConfigIdFK = this.minorStageConfig.getPrimaryKey().getId();
      }

      return(this.minorStageConfigIdFK);
   }


   public void setMinorStageConfigIdFK(final Integer minorStageConfigIdFK_)
   {
      this.minorStageConfigIdFK = minorStageConfigIdFK_;
   }


   //
   // Object override methods
   //

   @Override
   public boolean equals(final Object obj_)
   {
      // if passed object is this object
      if(this == obj_)
      {
         // they are equal
         return(true);
      }

      // if object is null or not AgentPK
      if(obj_ == null || !(obj_ instanceof AgentPK))
      {
         // they are not equal
         return(false);
      }

      final AgentPK other = (AgentPK)obj_;

      // if this id is null
      if(this.id == null)
      {
         // and the other id is not
         if(other.id != null)
         {
            // they are not equal
            return(false);
         }
      }
      // else if ids are not equal
      else if(!this.id.equals(other.id))
      {
         // they are not equal
         return(false);
      }

      // if this minor stage foreign key is null
      if(this.minorStageIdFK == null)
      {
         // and other minor stage foreign key is not
         if(other.minorStageIdFK != null)
         {
            // they are not equal
            return(false);
         }
      }
      // else if minor stage foreign key are not the same
      else if(!this.minorStageIdFK.equals(other.minorStageIdFK))
      {
         // they are not equal
         return(false);
      }

      // if this minor stage config foreign key is null
      if(this.minorStageConfigIdFK == null)
      {
         // and other minor stage config foreign key is not
         if(other.minorStageConfigIdFK != null)
         {
            // they are not equal
            return(false);
         }
      }
      // else if minor stage config foreign key are not the same
      else if(!this.minorStageConfigIdFK.equals(other.minorStageConfigIdFK))
      {
         // they are not equal
         return(false);
      }

      // they are equal
      return(true);
   }

   public int hashCode()
   {
      final int prime = 31;
      int result = 17;

      result = prime * result + (this.id == null ? 0 : this.id.hashCode());
      result = prime * result + (this.minorStageIdFK == null ? 0 : this.minorStageIdFK.hashCode());
      result = prime * result + (this.minorStageConfigIdFK == null ? 0 : this.minorStageConfigIdFK.hashCode());
      return(result);
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.