0 Replies Latest reply on Jul 22, 2005 4:26 AM by Ronald Mathies

    FetchType.LAZY : LEFT JOIN FETCH

    Ronald Mathies Newbie

      I hope this question is easy to awnser but it got me kind of confused:

      I've got two entity's:

      JOperator, JParameter.

      JOperator can have a collection of JParameter objects.
      The LAZY setting is set on the JParameter to a third entity wich is not realy interesting so it isn't shown here.

      Entity #1: JOperator

      package com.jinstance.javalibrary.j2ee.ejb;
      
      ...
      
      @Entity
      @Table(name = "operator")
      @NamedQueries(
       {
       @NamedQuery(name="joperator.findByPackageAndObjectAndOperator",
       queryString="SELECT OBJECT(j) FROM JOperator AS j" +
       " LEFT JOIN FETCH j.JParameters.typeObject " +
       " WHERE j.name = :operatorName" +
       " AND j.JObject.name = :objectName" +
       " AND j.JObject.JPackage.name = :packageName")
       }
       )
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING)
      @DiscriminatorColumn(name = "operatorType", length=16)
      public class JOperator extends EntityBase implements java.io.Serializable {
      
       private static final long serialVersionUID = 1L;
      
       public static final String NAMEDQUERY_FIND_ALL_BY_PACKAGE_AND_OBJECT_AND_OPERATOR = "joperator.findByPackageAndObjectAndOperator";
      
       private Long key;
       private Long version;
      
       private String name;
      
       private Collection<JParameter> jParameters;
      
       public JOperator() {
       }
      
       public JOperator(String name) {
       this.name = name;
       }
      
       @Id(generate=GeneratorType.AUTO)
       @Column(name="id")
       public Long getKey() {
       return this.key;
       }
      
       @Version
       @Column(name="version", nullable=false)
       public Long getVersion() {
       return version;
       }
      
       public void setVersion(Long version) {
       this.version = version;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       @Column(name="name", nullable=false)
       public String getName() {
       return this.name;
       }
      
       /******************************************************************
       * Parameters
       ******************************************************************/
       @OneToMany(fetch=FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy="JOperator")
       public Collection<JParameter> getJParameters() {
       if (jParameters == null) {
       jParameters = new ArrayList<JParameter>();
       }
       return jParameters;
       }
      
       public void setJParameters(Collection<JParameter> jParameters) {
       this.jParameters = jParameters;
       }
      
      }
      
      


      Entity #2: JParameter
      package com.jinstance.javalibrary.j2ee.ejb;
      
      ...
      
      @Entity
      @Table(name = "parameter")
      public class JParameter extends EntityBase implements java.io.Serializable {
      
       private static final long serialVersionUID = 1L;
      
       private Long key;
       private Long version;
      
       private String name;
      
       private JObject typeObject;
      
       private JOperator jOperator;
      
       public JParameter(String name) {
       this.name = name;
       }
      
       public void setKey(Long key) {
       this.key = key;
       }
      
       @Id(generate=GeneratorType.AUTO)
       @Column(name="id")
       public Long getKey() {
       return this.key;
       }
      
       @Version
       @Column(name="version", nullable=false)
       public Long getVersion() {
       return version;
       }
      
       public void setVersion(Long version) {
       this.version = version;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       @Column(name="name", nullable=false)
       public String getName() {
       return name;
       }
      
       @ManyToOne(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
       @JoinColumn(name = "typeObjectKey")
       public JObject getTypeObject() {
       return typeObject;
       }
      
       public void setTypeObject(JObject typeObject) {
       this.typeObject = typeObject;
       }
      
       @ManyToOne(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
       @JoinColumn(name = "operatorKey")
       public JOperator getJOperator() {
       return jOperator;
       }
      
       public void setJOperator(JOperator jOperator) {
       this.jOperator = jOperator;
       }
      
      }
      


      Know if i call the find method on the following stateless session bean:
      @Stateless
      public class JOperatorAgent implements JOperatorAgentRemote, JOperatorAgentLocal {
      
       @SuppressWarnings({"unchecked"})
       public JOperator find(String packageName, String objectName, String operatorName, String ... _parameters) throws AgentException {
       try {
       ...
      
       List<JOperator> operators = (List<JOperator>)manager.createNamedQuery(NAMEDQUERY_FIND_ALL_BY_PACKAGE_AND_OBJECT_AND_OPERATOR)
       .setParameter("packageName", packageName)
       .setParameter("objectName", objectName)
       .setParameter("operatorName", operatorName)
       .getResultList();
      
       ...
      
       } catch (GenericJDBCException e) {
       e.printStackTrace();
       } catch (EntityNotFoundException e) {
       e.printStackTrace();
       }
       }
      }
      


      The query runs just fine and doesn't give me any error message's. But the result isn't what i expected it to be, the typeObject property on the JParameter object is not filled. Why doesn't the LEFT JOIN FETCH work?

      If you don't know the awnser to this but you do know were i can find a good example with a LEFT JOIN FETCH then i am olso happy.