Jesse Jashinsky
Jesse Jashinsky

Reputation: 10663

criteria.createAlias() Multiplies Data

I'm using the following code to retrieve my data

Criteria cr = session.createCriteria( JobProcess.class );
cr.createAlias("sendMarcs", "sm");
cr.addOrder( Order.desc( "processedTime") );
cr.addOrder( Order.asc( "sm.ats" ) );

It returns all the data, but all the data appears multiple times, returning a list of size 673 when it should be a list of size 99. I have no idea why it's happening.

And yes, I've looked at the SQL that hibernate is using and it's correct.

Also, if I remove the alias and its order, then the data appears as expected. Not multiplied, but not sorted.

JobProcess.java

@Entity
@Table(name = "jobprocess")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class JobProcess {

    @Id
    @GeneratedValue
    private Long id;

    @Basic(optional = false)
    @Column(length = 20, name="customerid")
    private String customerId;

    @Basic(optional=false)
    @Column(length = 100)
    private String ATS;

    @Basic(optional=true)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timeprocessed")
    private java.util.Date processedTime;

    @Basic(optional=false)
    @Column(length = 1)
    private String state;

    @Column(length = 300)
    private String notes;

    @Basic(optional = false)
    private long nos;

    @OneToMany(mappedBy = "jobprocessid")
    private Set<SendMarc> sendMarcs = new HashSet<SendMarc>();

    // getters and setters
}

SendMarc.java

@Entity
@Table( name = "sendmarc" )
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
public class SendMarc
{
    @Id
    @GeneratedValue
    private BigInteger id;

    @Basic( optional = true )
    private BigInteger marcfileid;

    @Basic( optional = false )
    private long jobprocessid;

    @Basic( optional = false )
    @Column( length = 50 )
    private String ats;

    // getters and setters
}

Upvotes: 1

Views: 2489

Answers (2)

Art Licis
Art Licis

Reputation: 3679

#createAlias() creates alias for an association it also joined. In your case, without it you simply retrieve all rows from JobProcess table with no joins, and in case of additional join you get different result.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691805

It's returning one object per retrieved row, as a HQL query without distinct would do. WIth a Criteria, you must use

cr.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

Upvotes: 2

Related Questions