I need help please.
I am trying to use the Criteria API with a many to many entity class. I would like to be able to use non-primary key properties of one of the one-to-many key entities. I keep getting property not found exceptions... is there any way to do this? Thanks!
Hibernate version:
3.2
Annotated Mapping Classes:
Code:
@Entity
@Table(name = "region")
public class Region implements java.io.Serializable {
private Long id;
private String name;
private Status status;
private Region parentRegion;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "name", nullable = false, length = 150)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Enumerated(EnumType.STRING)
@Column(name="status")
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
public Region getParentRegion() {
return this.parentRegion;
}
public void setParentRegion(Region parentRegion) {
this.parentRegion = parentRegion;
}
}
Code:
@Entity
@IdClass(SubRegionsPK.class)
@Table(name = "sub_regions", catalog = "interview")
public class SubRegions {
private Region region;
private Region subRegion;
@Id
@ManyToOne
@JoinColumn(name = "region_id", nullable = false)
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
@Id
@ManyToOne
@JoinColumn(name = "sub_region_id", nullable = false)
public Region getSubRegion() {
return subRegion;
}
public void setSubRegion(Region subRegion) {
this.subRegion = subRegion;
}
}
Code:
@Embeddable
public class SubRegionsPK implements Serializable {
private Region region;
private Region subRegion;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "region_id", nullable = false)
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sub_region_id", nullable = false)
public Region getSubRegion() {
return subRegion;
}
public void setSubRegion(Region subRegion) {
this.subRegion = subRegion;
}
}
Code between sessionFactory.openSession() and session.close():Code:
Criteria crit = getSession().createCriteria(SubRegions.class);
crit.setProjection(Projections.property("subRegion"))
.add(Expression.eq("region.id", user.getRegion().getId()))
.add(Expression.eq("subRegion.status", Status.ACTIVE));
return crit.list();
Full stack trace of any exception that occurs:
Quote:
org.hibernate.QueryException: could not resolve property: subRegion.status of: com.interview.model.entity.general.SubRegions
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:59)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1354)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:394)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:79)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:59)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
...
Name and version of the database you are using:Mysql 5.1
The generated SQL (show_sql=true):N/A
Debug level Hibernate log excerpt:Quote:
20:07:27,281 DEBUG [SessionImpl] initializing proxy: [com.interview.model.entity.general.User#1]
20:07:27,281 DEBUG [Loader] loading entity: [com.interview.model.entity.general.User#1]
20:07:27,281 DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
20:07:27,281 DEBUG [ConnectionManager] opening JDBC connection
20:07:27,281 DEBUG [SQL] select user0_.id as id11_0_, user0_.created_date as created2_11_0_, user0_.email_address as email3_11_0_, user0_.first_name as first4_11_0_, user0_.last_modified_date as last5_11_0_, user0_.last_name as last6_11_0_, user0_.organization_id as organiz13_11_0_, user0_.password as password11_0_, user0_.perm_interview as perm8_11_0_, user0_.perm_position as perm9_11_0_, user0_.perm_region as perm10_11_0_, user0_.perm_user as perm11_11_0_, user0_.region_id as region14_11_0_, user0_.status as status11_0_ from interview.user user0_ where user0_.id=?
20:07:27,296 INFO [STDOUT] Hibernate: select user0_.id as id11_0_, user0_.created_date as created2_11_0_, user0_.email_address as email3_11_0_, user0_.first_name as first4_11_0_, user0_.last_modified_date as last5_11_0_, user0_.last_name as last6_11_0_, user0_.organization_id as organiz13_11_0_, user0_.password as password11_0_, user0_.perm_interview as perm8_11_0_, user0_.perm_position as perm9_11_0_, user0_.perm_region as perm10_11_0_, user0_.perm_user as perm11_11_0_, user0_.region_id as region14_11_0_, user0_.status as status11_0_ from interview.user user0_ where user0_.id=?
20:07:27,312 DEBUG [AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
20:07:27,312 DEBUG [Loader] result row: EntityKey[com.interview.model.entity.general.User#1]
20:07:27,328 DEBUG [AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)
20:07:27,328 DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
20:07:27,343 DEBUG [TwoPhaseLoad] resolving associations for [com.interview.model.entity.general.User#1]
20:07:27,343 DEBUG [TwoPhaseLoad] done materializing entity [com.interview.model.entity.general.User#1]
20:07:27,343 DEBUG [StatefulPersistenceContext] initializing non-lazy collections
20:07:27,343 DEBUG [Loader] done entity load