title: hibernate criteria.setResultTransformer() with projections.distinct (composite foreign keys)
body: <p>I have SQL:</p>
  
  <pre><code>SELECT DISTINCT archiveSer, clas, classifdegree
  FROM realization_archive
  WHERE realId = 2
  ORDER BY archiveSer DESC
  </code></pre>
  
  <p>My tables are mapped in hibernate as follows:</p>
  
  <p>main table (realization_archive):</p>
  
  <pre><code>public class RealizationArchive implements Serializable {
  @Id
  @Access(AccessType.PROPERTY)
  @Column(name = "archiveSer", nullable = false)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long archiveSerial;
  
  @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
  @JoinColumn(name = "realId", referencedColumnName = "realId", nullable = false)
  private CreditRealization creditRealization;
  
  @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, optional = true)
  @JoinColumns({ @JoinColumn(name = "classif", referencedColumnName = "classif"), @JoinColumn(name = "classifdegree", referencedColumnName = "classifdegree") })
  private ClassificationDegree classification;
  
  ...
  }
  </code></pre>
  
  <p>object which is composite foreign key in main table RealizationArchive (realization_archive) </p>
  
  <pre><code>public class ClassificationDegree implements Serializable{
  @Id
  @Access(AccessType.PROPERTY)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false)
  private Long id;
  
  @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, optional = false)
  @JoinColumn(name = "classif", referencedColumnName = "clas", nullable = false)
  private ClassificationLevel classificationLevel;
  
  @Column(name = "classifdegree")
  private Integer classificationDegree;
  ...
  }
  </code></pre>
  
  <p>simple foregin key in ClassificationDegeree</p>
  
  <pre><code>public class ClassificationLevel implements Serializable {
  @Id
  @Access(AccessType.PROPERTY)
  @Column(name = "clas", nullable = false)
  private Integer classificationLevelId;
  
  ...
  }
  </code></pre>
  
  <p>simple foreign key in RealizationArchive</p>
  
  <pre><code>public class CreditRealization implements Serializable{
  @Id
  @Access(AccessType.PROPERTY)
  @Column(name = "realId", nullable = false)
  private Long creditRealizationId;
  ...
  }
  </code></pre>
  
  <p>I am trying to do previously mentioned SQL query with criteria like this:</p>
  
  <pre><code>Criteria criteria = getSession().createCriteria(RealizationArchive.class);
          criteria.add(Restrictions.eq("creditRealization.creditRealizationId", creditRealizationId));
          criteria.addOrder(Order.desc("archiveSerial"));
          criteria.setProjection(Projections.distinct(Projections.projectionList().add(Projections.property("creditRealization").as("creditRealization"))
                  .add(Projections.property("archiveSerial").as("archiveSerial"))
                  .add(Projections.property("classification").as("classification"))));
          criteria.setMaxResults(1);
  
          criteria.setResultTransformer(Transformers.aliasToBean(RealizationArchive.class));
          return criteria.uniqueResult();
  }
  </code></pre>
  
  <p>BUT, my problem is that in the object that is returned (RealizationArchive) values for fields 
      RealizationArchive.classification.classificationDegree 
  and
      RealizationArchive.classification.classificationLevel.classificationLevelId
  are not loaded properly. (they have null values, but in the db they are e.g 0, 1).
  It works for simple foreign key (e.g for creditRealization [realId]), but when I have composite foreign key (e.g for classification [classif, classifdegree]) values are not loaded ... Bottom line, my SQL query generates just fine (syntactically), BUT returning object has all values set corectlly, EXCEPT those one that are part of the composite foreign key (that is, classificationLevelId(db name - clas) and classificationDegree (db name - clasifdegree)) ...</p>
  
  <p>Can this be done by criteria and if so, how? I don't get it, what am I doing wrong?
  Any kind of help is appreciated, thanks in advance for your time and patience :)</p>
  
  <p>mismas</p>