title: Hibernate upgrade from 3 to 4: Criteria transaction ceases to work
body: <p>I have recently upgraded Spring 2.5 to 3.2 and Hibernate 3 to 4.2.8 in a general upgrade of a web application. Most things are working now, but there is one Criteria transaction that is not working and has me puzzled. The new version returns no result (but no errors), while the old one retrieved properly the requested value.</p>
  
  <p>The code is the same one in the old and new versions, and I have verified that the argument that reaches it is the same. Here is the Java code:</p>
  
  <pre><code>Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ViewingResource.class);
  criteria.createCriteria("viewings","currentViewings");
  criteria.add(Property.forName("currentViewings.id").eq(viewingId));
  
  ViewingResource result = (ViewingResource)criteria.uniqueResult();
  </code></pre>
  
  <p>ViewingResource is my entity, which is defined as follows:</p>
  
  <pre><code>@Entity
  @DiscriminatorValue("viewing")
  public class ViewingResource extends AbstractInformationResource {  
  private static final long serialVersionUID = -4569093742552159052L;
  
  @OneToOne(targetEntity = Attribute.class, fetch = FetchType.LAZY)
  @JoinColumn
  private Attribute primaryAttribute;
  
  @OneToMany(targetEntity = Viewing.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval=true)
  @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
  @JoinTable(name = "informationresource_viewings")
  @OrderBy("sort")
  private Set&lt;ResourceViewing&gt; viewings;
  
  public Set&lt;ResourceViewing&gt; getViewings() {
      return viewings;
  }
  
  public Attribute getPrimaryAttribute() {
      return primaryAttribute;
  }
  }
  </code></pre>
  
  <p>As for the abstract class it extends:</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  @DiscriminatorColumn(
  name = "type",
  discriminatorType = DiscriminatorType.STRING
  )
  @Table(name = "informationresource")
  abstract public class AbstractInformationResource extends PersistentEntity&lt;String&gt; {
  private static final long serialVersionUID = 8709376067232042462L;
  
  @Id @GeneratedValue(generator="system-uuid")
  @GenericGenerator(name="system-uuid", strategy = "uuid")
  private String id;
  
  @Column(nullable = false)
  private String name;
  
  @Column(nullable = false)
  private int sort;
  
  public String getId() {
      return id;
  }
  
  public String getName() {
      return name;
  }
  
  public int getSort() {
      return sort;
  }
  }
  </code></pre>
  
  <p>And the original PersistentEntity is just an extension of Serializable with an id and no annotations.</p>
  
  <p>I enabled Hibernate logs and found the problem may be in the way annotations work between in Hibernate 3 and 4, for the Hibernate generated SQL strings differ in this way:</p>
  
  <p>Hibernate 3:</p>
  
  <pre><code>select
      ... (maps to all columns)
  from
      informationresource this_ 
  inner join
      informationresource_viewings viewings3_ 
          on this_.id=viewings3_.informationresource_id 
  inner join
      Viewing currentvie1_ 
          on viewings3_.viewings_id=currentvie1_.id 
  where
      this_.type in (
          'viewing', 'directory'
      ) 
      and currentvie1_.id=?
  </code></pre>
  
  <p>Whereas in Hibernate 4, the generated SQL performs no joins:</p>
  
  <pre><code>select
      ... (maps to all columns, except type, attributeType and fieldName)
  from 
      informationresource this_, 
      informationresource_viewings viewings3_, 
      Viewing currentvie1_ 
  where 
      this_.id=viewings3_.informationresource_id 
      and viewings3_.viewings_id=currentvie1_.id 
      and this_.type='viewing' 
      and currentvie1_.id=?
  </code></pre>
  
  <p>Any hints that may help me advance with this issue? My current guess is that maybe I skipped some annotation definition that has been changed or modified since Hibernate 3, but so far I haven't been able to find anything illegal in the way I declare them - and my attempts to modify the @Join have been unsuccessful so far. </p>
  
  <p>EDIT. After toying with this for some time, I have found that the issue may be related to the @DiscriminatorColumn of the abstract class. I have found that the problem lies that my type for this kind of request is never 'viewing', but 'directory'. In the old generated SQL I had both types generated:</p>
  
  <pre><code>this_.type in (
      'viewing', 'directory'
  )
  </code></pre>
  
  <p>But in the new sql this is constrained to 'viewing':</p>
  
  <pre><code>and this_.type='viewing'
  </code></pre>
  
  <p>I have changed in the new SQL this line, and it returns the right values that I need. The column type has only those two values, 'viewing' and 'directory'. So my question now is how to make Criteria to keep asking for the types there instead of forcing 'viewing' type.</p>