2010-03-23 100 views
1

我想了解它是如何可能: ,直到我用一个表一切工作运行良好, 时候我已经映射失败的另一个表,如下图所示:休眠失败映射两个表

Glassfish的开始

INFO: configuring from resource: /hibernate.cfg.xml 
INFO: Configuration resource: /hibernate.cfg.xml 
INFO: Reading mappings from resource : hibernate_centrale.hbm.xml //first table 
INFO: Mapping class: com.italtel.patchfinder.objects.centrale -> centrale 
INFO: Reading mappings from resource : hibernate_impianti.hbm.xml //second table 
INFO: Mapping class: com.italtel.patchfinder.objects.Impianto -> impianti 
INFO: Configured SessionFactory: null 


INFO: schema update complete 
INFO: Hibernate: select centrale0_.id as id0_, centrale0_.name as name0_, centrale0_.impianto as impianto0_, centrale0_.servizio as servizio0_ from centrale centrale0_ group by centrale0_.name 
INFO: Hibernate: select centrale0_.id as id0_, centrale0_.name as name0_, centrale0_.impianto as impianto0_, centrale0_.servizio as servizio0_ from centrale centrale0_ where centrale0_.name='ANCONA' order by centrale0_.name asc 

//Error 
org.hibernate.hql.ast.QuerySyntaxException: impianti is not mapped [from impianti where impianto='SD' order by modulo asc] 
     at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) 
... 

配置

table1的

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> 
    <class name="com.italtel.patchfinder.objects.Impianto" table="impianti"> 
     <id column="id" name="id"> 
      <generator class="increment"/> 
     </id> 
     <property name="impianto"/> 
     <property name="modulo"/> 
    </class> </hibernate-mapping> 

表2

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="com.italtel.patchfinder.objects.centrale" table="centrale"> 
     <id column="id" name="id"> 
      <generator class="increment"/> 
     </id> 
     <property name="name"/> 
     <property name="impianto"/> 
     <property name="servizio"/> 
    </class> 
</hibernate-mapping> 

连接东西 ...

<property name="hbm2ddl.auto">update</property> 
    <mapping resource="hibernate_centrale.hbm.xml"/> 
    <mapping resource="hibernate_impianti.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

`

public List<centrale> loadAll() { 
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     return session.createQuery("from centrale group by name").list(); 
    } 

    public List<centrale> loadImplants(String centrale) { 
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     return session.createQuery("from centrale where name='" + centrale + "' order by name asc").list(); 
    } 

    public List<Impianto> loadModules(String implant) { 
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     return session.createQuery("from impianti where impianto='" + implant + "' order by modulo asc").list(); 
    } 
} 

你有一些建议吗?

+1

你能解决这个问题吗? – Jeremy 2010-03-23 15:54:30

回答

2

根据你的映射,你的类名是Impianto,所以你的查询应该是“来自Impianto ...”而不是“来自impianti ...” - 从类名选择,而不是表名。

+0

准备发布这个。另外,为了保持一致性,最好大写你的类名。 – Jeremy 2010-03-23 19:40:33

+0

是的!谢谢你们,我纠正了任何错误。 – sebbalex 2010-03-24 00:58:40