简体   繁体   English

如何正确使用休眠注释?

[英]How to use hibernate annotation properly?

I just set up a basic hibernate/spring project to test some stuff. 我只是建立了一个基本的hibernate / spring项目来测试一些东西。 I use a MySQL db by using WAMP. 我通过使用WAMP使用MySQL数据库。

These are all of the classes: http://codepaste.net/7pwmtx 这些是所有类: http : //codepaste.net/7pwmtx

This is my bean definition and pom file: http://codepaste.net/4iz7jb 这是我的bean定义和pom文件: http : //codepaste.net/4iz7jb

POM is a little bit messy but this is the problem: I think that I set up my dialect properly because I get no errors when i add something to the db. POM有点混乱,但这是问题所在:我认为我已经正确设置了方言,因为在向数据库中添加内容时没有错误。 I get errors when I use find in hql. 在hql中使用find时出现错误。 I t seems like hibernate isnt retrieving data from the same place that it is putting it. 看来冬眠并没有从放置数据的地方检索数据。 Any idea why this doesnt work? 任何想法为什么这不起作用?

By the way, the console output was: 顺便说一句,控制台输出为:

Exception in thread "main" org.springframework.orm.hibernate3.HibernateQueryException: hobject is not mapped [from hobject]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: hobject is not mapped [from hobject]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
    at hibernate.dao.SpringHibernateOperatorImplementation.getAllRows(SpringHibernateOperatorImplementation.java:24)
    at hibernate.main.HibernateMain.main(HibernateMain.java:22)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: hobject is not mapped [from hobject]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    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:251)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    ... 5 more

Add the entity class hibernate.dao.HObject to hibernate.cfg.xml files. 将实体类hibernate.dao.HObject添加到hibernate.cfg.xml文件中。 It is neeeded even if you are using annotation based configurations. 即使您使用的是基于注释的配置,也必须这样做。

If you want to skip adding these to cfg file you can use the approach mentioned here provided you are using spring . 如果您想跳过将这些添加到cfg文件中,可以使用此处提到的方法前提是您正在使用spring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM