问题不大,但如果不知道根源,也会浪费很多无辜的时间。3 K# R' p) ~4 g( l _) ?
. [' R) g/ ^1 T
# B# J( F9 H9 ]- [$ ]& H3 {2 A3 y( O& Z$ ?* E+ A' M3 z( Q
异常信息: + X- i" U& c4 ~& x . P* ], m, a9 R. w8 |/ ^* t' l# M5 l/ J$ W
Caused by: org.hibernate.DuplicateMappingException: duplicate import: TUser4 refers to both com.alcatel.cn.hibernate.ORMapping.onetomany.pojo.TUser4 and com.alcatel.cn.hibernate.chaper04.pojo.TUser4 (try using auto-import="false") + s- h, c/ h- y: G/ b at org.hibernate.cfg.Mappings.addImport(Mappings.java:141) $ s6 y5 [2 o, ?. H) O* K: c- P9 P3 D at org.hibernate.cfg.HbmBinder.bindPersistentClassCommonValues(HbmBinder.java:614) , X. R9 o" m, e2 h& u0 w at org.hibernate.cfg.HbmBinder.bindClass(HbmBinder.java:527)" C9 E+ r- m5 c* G
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:271) / g( G' d' s# K1 Y6 i( t$ n at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)" R }3 _% }2 u* T- @! R. V6 f
at org.hibernate.cfg.Configuration.add(Configuration.java:424) 0 ?9 A4 g h4 m/ E5 B at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:465)2 R" H( E' H: o B
at org.hibernate.cfg.Configuration.addResource(Configuration.java:520)+ @* O8 W& B) Q: x& P9 }
... 8 more " x8 ]5 _: s; k7 \' b9 |- W0 L7 n8 }. r
& X* `* [1 w9 I0 U& z+ ] 1 L9 x N @& R9 A2 Y! Y R2 y 4 y0 }# @9 }. `造成这个异常的原因可能是: ) p& S7 {+ @& z1 g # K+ J. V4 y2 f, u! Y在你的工程里面,有两个TUser4类,比如:cn.com.test01.TUser4与cn.com.Test02.TUser4,虽然位于不同的包,但是会出现 duplicate import异常,解决办法如下:/ `& f4 z$ D, g5 }6 m
5 P1 F7 L1 Q( H a b
1:比较简单的办法,更改其中一个类名。1 p3 T+ }- N( n' _; E' _
7 ~, `; [! V/ N }+ Z6 l- H2:第二种解决办法,将类TUser4的映射文件,<hibernate-mapping>结点后加上auto-import="false",默认为true,即变成<hibernate-mapping auto-import="false">,但是这样改了以后,千万要注意,在写HQL语句时候,比如"from cn.com.test01.TUser4",一定要加上完整包名,因为你设置了auto-import="false". " [0 U9 h1 {5 }4 B; h! M! Y4 I; H U+ H2 z6 l! z
( s- @- k( M3 Z