I can't figure out how to do an 'order by' for a field that is declared in an @Embedded object.
Hibernate version:
hibernate 3.1.1
hibernate annotations 3.1beta7
Mapping documents:
I'm using annotations.
Code snippet from Product class
Code:
public class Product implements Serializable {
@Id(generate = GeneratorType.AUTO)
private Long id;
@Version
private Integer version;
@Embedded
private BasicDescriptor basicDescriptor = new BasicDescriptor();
Code snippet from BasicDescriptor
Code:
@Embeddable(access = AccessType.FIELD)
public class BasicDescriptor {
private String name;
private String shortDescription;
private String longDescription;
Code between sessionFactory.openSession() and session.close():Using Spring 1.2.6, the method in my DAO is
Code:
public List <Product> getProducts(int pageNumber, int resultsPerPage) {
int firstResult = (pageNumber - 1) * resultsPerPage;
Criteria criteria = getHibernateTemplate().getSessionFactory().openSession().createCriteria(Product.class);
criteria.addOrder(Order.asc("name"));
criteria.setFirstResult(firstResult);
criteria.setMaxResults(resultsPerPage);
List <Product> allProducts = criteria.list();
return allProducts;
}
Full stack trace of any exception that occurs:Code:
Exception in thread "main" org.hibernate.QueryException: could not resolve property: name of: com.gocontent.shop.domainlayer.model.product.Product
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:409)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:371)
at org.hibernate.criterion.Order.toSqlString(Order.java:45)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:328)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:86)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1531)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at com.gocontent.shop.domainlayer.daos.hibernate.ProductDAOHibernate.getProducts(ProductDAOHibernate.java:41)
at com.gocontent.shop.applicationlayer.services.impl.VisitorServicesImpl.listProducts(VisitorServicesImpl.java:93)
at com.gocontent.shop.interfacelayer.commandline.VisitorServicesRunner.main(VisitorServicesRunner.java:65)
Name and version of the database you are using:PostgreSQL 7.4.3
The generated SQL (show_sql=true):No SQL generated for the statement causing the problem, full output below...
Debug level Hibernate log excerpt:Have enabled 'debug', the total output is (although I don't see any 'DEBUG' messages...)
Code:
08-Feb-2006 15:47:45 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
08-Feb-2006 15:47:46 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=1102920]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [devAndrewDataSource,devAndrewHibernateSessionFactory,productDAOHibernate,productDataAssembler,productSummaryDataAssembler,visitorServices,servicesFactory]; root of BeanFactory hierarchy
08-Feb-2006 15:47:46 org.springframework.context.support.AbstractApplicationContext refresh
INFO: 7 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=1102920]
08-Feb-2006 15:47:46 org.springframework.core.CollectionFactory <clinit>
INFO: JDK 1.4+ collections available
08-Feb-2006 15:47:46 org.springframework.context.support.AbstractApplicationContext initMessageSource
INFO: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1186fab]
08-Feb-2006 15:47:46 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
INFO: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1d5550d]
08-Feb-2006 15:47:46 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [devAndrewDataSource,devAndrewHibernateSessionFactory,productDAOHibernate,productDataAssembler,productSummaryDataAssembler,visitorServices,servicesFactory]; root of BeanFactory hierarchy]
08-Feb-2006 15:47:46 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
08-Feb-2006 15:47:46 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.1.1
08-Feb-2006 15:47:46 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
08-Feb-2006 15:47:46 org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
08-Feb-2006 15:47:46 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
08-Feb-2006 15:47:46 org.springframework.orm.hibernate3.LocalSessionFactoryBean afterPropertiesSet
INFO: Building new Hibernate SessionFactory
08-Feb-2006 15:47:46 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: java.lang.Class
08-Feb-2006 15:47:47 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity com.gocontent.shop.domainlayer.model.product.Product on table Product
08-Feb-2006 15:47:47 org.hibernate.connection.ConnectionProviderFactory newConnectionProvider
INFO: Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: PostgreSQL, version: 7.4.3
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1 JDBC3 with SSL (build 404)
08-Feb-2006 15:47:48 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.PostgreSQLDialect
08-Feb-2006 15:47:48 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
08-Feb-2006 15:47:48 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: on_close
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
08-Feb-2006 15:47:48 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.EhCacheProvider
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
08-Feb-2006 15:47:48 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
08-Feb-2006 15:47:48 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
08-Feb-2006 15:47:48 net.sf.ehcache.config.Configurator configure
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/M:/m2repository/ehcache/ehcache/1.1/ehcache-1.1.jar!/ehcache-failsafe.xml
08-Feb-2006 15:47:49 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: Running hbm2ddl schema update
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: fetching database metadata
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: updating schema
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: table found: public.product
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: columns: [longdescription, name, shortdescription, id, version]
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: foreign keys: []
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: indexes: [product_pkey]
08-Feb-2006 15:47:49 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
08-Feb-2006 15:47:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
08-Feb-2006 15:47:50 org.springframework.jdbc.support.SQLErrorCodesFactory <init>
INFO: SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Product (version, name, shortDescription, longDescription, id) values (?, ?, ?, ?, ?)
Hibernate: select product0_.id as id0_0_, product0_.version as version0_0_, product0_.name as name0_0_, product0_.shortDescription as shortDes4_0_0_, product0_.longDescription as longDesc5_0_0_ from Product product0_ where product0_.id=?
Hibernate: select product0_.id as id0_0_, product0_.version as version0_0_, product0_.name as name0_0_, product0_.shortDescription as shortDes4_0_0_, product0_.longDescription as longDesc5_0_0_ from Product product0_ where product0_.id=?
Hibernate: update Product set version=?, name=?, shortDescription=?, longDescription=? where id=? and version=?
Exception in thread "main" org.hibernate.QueryException: could not resolve property: name of: com.gocontent.shop.domainlayer.model.product.Product
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:409)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:371)
at org.hibernate.criterion.Order.toSqlString(Order.java:45)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:328)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:86)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1531)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at com.gocontent.shop.domainlayer.daos.hibernate.ProductDAOHibernate.getProducts(ProductDAOHibernate.java:41)
at com.gocontent.shop.applicationlayer.services.impl.VisitorServicesImpl.listProducts(VisitorServicesImpl.java:93)
at com.gocontent.shop.interfacelayer.commandline.VisitorServicesRunner.main(VisitorServicesRunner.java:65)
What I've tried
If I remove the 'criteria.addOrder(Order.asc("name"));' then everything works fine.
If I move the 'name' field into the Product class then everything works fine.
I just can't figure out how to reference name when it is embedded.
Any help would be really appreciated.
Thanks
Andrew