Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Issue with Criteria Query and Inheritance
PostPosted: Fri Jun 01, 2012 11:36 am 
Newbie

Joined: Fri Jun 01, 2012 11:14 am
Posts: 2
I am attempting to build a Criteria query and seem to be having trouble accessing some of the inherited attributes of the subclass when the attribute is another entity. My setup looks like so:

3 entities - MerchantReport extends ReportInstance with has another entity, ReportConfig, as an attribute. the three entities are set up as follows:

@Entity
@Inheritance
@DiscriminatorColumn(name = "ReportType")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Table(name = "ReportInstance")
public abstract class ReportInstance implements java.io.Serializable {

protected Integer id;
protected ReportConfig reportConfig;
protected String format;
protected boolean active;
protected String reportType;
protected Set<ReportParameter> reportParameters = new HashSet<ReportParameter>(0);

protected Set<TransferMethod> transferMethods = new HashSet<TransferMethod>(0);

:
:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ReportConfigId", nullable = false)
public ReportConfig getReportConfig() {
return this.reportConfig;
}
:
:
......
@Entity
@DiscriminatorValue("Merchant")
@XmlRootElement(name = "merchantReport")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class MerchantReport extends ReportInstance {

private Merchant merchant;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "MerchantId")
public Merchant getMerchant() {
return this.merchant;
}
.......

@Entity
@XmlRootElement(name = "reportConfig")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Table(name = "ReportConfig", uniqueConstraints = @UniqueConstraint(columnNames = "Name"))
public class ReportConfig implements java.io.Serializable {

private Integer id;
private String name;
private String displayName;
private String fileName;
private String sasRepository;
private String fileNamePattern;
private String notificationEmail;


The skinnied down version of the Criteria I am building looks like this:

Session session = (Session) entityManager.getDelegate();
Criteria crit = session.createCriteria(MerchantReport.class);

crit.setFetchMode("merchant", FetchMode.SELECT);
crit.setFetchMode("reportConfig", FetchMode.SELECT);

crit.setMaxResults(20);
crit.setFirstResult(1);

Criteria merchantCrit = crit.createCriteria("merchant");
Criteria reportConfigCrit = crit.createCriteria("reportConfig");

if (SearchUtils.hasSearchValue(name)) {
merchantCrit.add(Restrictions.like("name", "%" + name + "%"));
}
merchantCrit.addOrder(Order.desc("name"));

reportConfigCrit.addOrder(Order.asc("displayName"));

List<MerchantReport> dbMerchantReports = crit.list();

The error I am getting is:

Caused by: org.hibernate.QueryException: could not resolve property: reportConfig.displayName of: com.cfna.mss.model.entity.MerchantReport

I am stumped. I can order by MerchantReport merchant.name but can't on the ReportInstance reportConfig.displayName. FetchMode on both the merchant and reportInstance are set to EAGER.


Top
 Profile  
 
 Post subject: Re: Issue with Criteria Query and Inheritance
PostPosted: Mon Jun 04, 2012 3:11 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
szvx4x wrote:

Session session = (Session) entityManager.getDelegate();
Criteria crit = session.createCriteria(MerchantReport.class);

crit.setFetchMode("merchant", FetchMode.SELECT);
crit.setFetchMode("reportConfig", FetchMode.SELECT);

crit.setMaxResults(20);
crit.setFirstResult(1);

Criteria merchantCrit = crit.createCriteria("merchant");
Criteria reportConfigCrit = crit.createCriteria("reportConfig");

if (SearchUtils.hasSearchValue(name)) {
merchantCrit.add(Restrictions.like("name", "%" + name + "%"));
}
merchantCrit.addOrder(Order.desc("name"));

reportConfigCrit.addOrder(Order.asc("displayName"));

List<MerchantReport> dbMerchantReports = crit.list();

The error I am getting is:

Caused by: org.hibernate.QueryException: could not resolve property: reportConfig.displayName of: com.cfna.mss.model.entity.MerchantReport

I am stumped. I can order by MerchantReport merchant.name but can't on the ReportInstance reportConfig.displayName. FetchMode on both the merchant and reportInstance are set to EAGER.



I'm pretty certain that projections and orders really only apply to the base Criteria.
I would try:
1. crit.addOrder(Order.asc("reportConfig.displayName"));

or

1. crit.createAlias("reportConfig", "reportConfig");
2. crit.addOrder(Order.asc("reportConfig.displayName"));


On another note, the Restrictions class provides a MatchMode class for LIKE queries. I would look into that.


Top
 Profile  
 
 Post subject: Re: Issue with Criteria Query and Inheritance
PostPosted: Tue Jun 05, 2012 10:36 am 
Newbie

Joined: Fri Jun 01, 2012 11:14 am
Posts: 2
That is the answer!! Thank you so much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

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.