Hi Guys.
As the n00b on the block, I am still bashing my head against new problems along the road to hiber-zen-nate.
I have just gone from the "Hello World" example in the "Java Persistence with Hibernate" where I used a hbm.xml file to map my example class;
Message class.
Now, trying to run the exact same example as a JPA project, using annotations and the Hibernate Entity Manager, but the query :
Quote:
"from Message m order by m.text asc"
throws an exception:
Quote:
"could not resolve type text..."
(see full stack trace below)
The text property is noted as
Code:
@Column(name="MESSAGE_TEXT")
private String _text;
- in my
Message class.
If I change the query to take the m._text property everything runs as it should.
I notice that when using the hbm mapping style, I mapped the property with both a name and a column:
Code:
<property name="text" column="MESSAGE_TEXT"></property>
But I guess that this is the point of using bean notation (getText and setText) thus letting "text" become a "property" of the mapped class?
I'm sorry if this question is stupid, but I really would like to learn why it is not working.
I thank you in advance for the help.
Cheers
Asger
Hibernate version: Hibernate 3.2.0.cr5
Hibernate EntityManager 3.2.0.CR3
Hibernate Annotations 3.2.0.CR3
Mapping documents:Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="hello_world_entity_manager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>hello.Message</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate_c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<property name="hibernate.hbm2dll.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
Code between sessionFactory.openSession() and session.close():NOTE Using the entity manager:
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello_world_entity_manager");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Message message = new Message("Hello World");
em.persist(message);
tx.commit();
em.close();
//Second Unit of work
EntityManager newEm = emf.createEntityManager();
EntityTransaction newTx = newEm.getTransaction();
newTx.begin();
List messages = newEm.createQuery("from Message m order by m.text asc").getResultList();
System.out.println(messages.size() + " message(s) found: ");
for(Iterator iter = messages.iterator(); iter.hasNext();)
{
Message loadedMsg =(Message)iter.next();
System.out.println(loadedMsg.getText());
}
newTx.commit();
newEm.close();
emf.close();
Full stack trace of any exception that occurs:Code:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: text of: hello.Message [from hello.Message m order by m.text asc]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:634)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:95)
at hello.HelloWorld.main(HelloWorld.java:47)
Caused by: org.hibernate.QueryException: could not resolve property: text of: hello.Message [from hello.Message m order by m.text asc]
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.toType(AbstractEntityPersister.java:1310)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:280)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:373)
at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:539)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:221)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:172)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:735)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.orderExprs(HqlSqlBaseWalker.java:1545)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.orderClause(HqlSqlBaseWalker.java:1521)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:622)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
... 1 more
Name and version of the database you are using:HSQLDB
The generated SQL (show_sql=true):Quote:
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| \-[FROM] 'from'
| \-[RANGE] 'RANGE'
| +-[DOT] '.'
| | +-[IDENT] 'hello'
| | \-[IDENT] 'Message'
| \-[ALIAS] 'm'
\-[ORDER] 'order'
+-[DOT] '.'
| +-[IDENT] 'm'
| \-[IDENT] 'text'
\-[ASCENDING] 'asc'