org.hibernate.MappingException
程序员文章站
2022-04-16 11:05:30
...
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: An association from the table author_book refers to an unmapped class: Author
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at po.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
at po.Test.select(Test.java:17)
at po.Test.main(Test.java:12)
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource po/Author.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at po.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at po.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at po.Test.select(Test.java:17)
at po.Test.main(Test.java:12)
Caused by: org.hibernate.DuplicateMappingException: Duplicate collection role mapping po.Author.bookSet
at org.hibernate.cfg.Mappings.addCollection(Mappings.java:124)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2066)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2041)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:359)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
at org.hibernate.cfg.Configuration.add(Configuration.java:669)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more
Exception in thread "main" java.lang.NullPointerException
at po.Test.select(Test.java:18)
at po.Test.main(Test.java:12)
修改的办法:
在多对多的关系中使用myeclipse容易出现这种错误:
比如:
<set name="authorSet" table="author_book" inverse="true" cascade="save-update" >
<key column="bid"/>
<many-to-many class="Author" column="aid"/>
</set>
在many-to-many 中class没有加上包名导致的以上错误:
修改如下:
<set name="authorSet" table="author_book" inverse="true" cascade="save-update" >
<key column="bid"/>
<many-to-many class="com.ch.army.Author" column="aid"/>
</set>