標籤:
錯誤1:MappingException: Unknown entity解決方案
http://jingyan.baidu.com/article/e75aca8552761b142edac6cf.html
錯誤2:Could not bind factory to JNDI
2006-07-23 23:14:53,796 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDIjavax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284) at javax.naming.InitialContext.getNameParser(InitialContext.java:439) at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52) at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176) at reene.study.hibernate.test.TestMain.main(TestMain.java:19)
在網上找到如下內容:
(3)報錯:Could not bind factory to JNDI
此錯誤是設定檔的問題,如果hibernate設定檔有session_factory_name這個變數,<session-factory name="foo"> 會試圖將一個SessionFactory執行個體以foo為名bind到jndi上,而有的application container不支援jndi綁定。把這個變數去掉即可。
解決問題
把引用代碼也要修改:
大致改成這樣:
SessionFactory sf = new Configuration().configure().buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();
錯誤3: entity class not found:
org.hibernate.MappingException: entity class not found: Customer at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:128) at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:390) at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:75) at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:145) at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:505) at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:144) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163) at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135) at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:386) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782) at com.session.Test.main(Test.java:15)Caused by: java.lang.ClassNotFoundException: Customer at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:190) at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192) at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:125) ... 15 more
修改Customer.hbm.xml
原來class name
<class name="Customer" table="CUSTOMER"> <id name="id" column="CID" type="java.lang.Integer"> <generator class="increment" /> </id> <property name="username" column="USERNAME" type="java.lang.String"/> <property name="password" column="PASSWORD" type="java.lang.String"/> </class>
修改為
<class name="com.session.Customer" table="CUSTOMER"> <id name="id" column="CID" type="java.lang.Integer"> <generator class="increment" /> </id> <property name="username" column="USERNAME" type="java.lang.String"/> <property name="password" column="PASSWORD" type="java.lang.String"/> </class>
錯誤4:Attribute "column" must be declared for element type "property"解決辦法
自己寫的xxx.hbm.xml檔案 出現錯誤:
Attribute "column" must be declared for element type"property"
這個錯誤是因為自己把hibernate.cfg.xml的檔案頭直接複製到了xxx.hbm.xml(在這裡為Customer.hbm.xml)中
hibernate.cfg.xml的檔案頭:
<?xmlversion=‘1.0‘ encoding=‘UTF-8‘?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
這個應該改為mapping的標頭檔就行了
Customer.hbm.xml的檔案頭
<?xmlversion="1.0" encoding="UTF-8"?><!DOCTYPEhibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
然後在裡面編寫自己的映射就行了。
錯誤5:not found while looking for property錯誤
not found while looking for property錯誤
Caused by: org.hibernate.MappingException: com.session.Customer not found while looking for property: id
設定檔如下
<?xml version="1.0" encoding="utf-8" ?><!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.session.Customer" table="CUSTOMER"> <id name="id" column="CID"> <generator class="increment" /> </id> <property name="username" column="USERNAME"/> <property name="password" column="PASSWORD"/> </class></hibernate-mapping>
發現自己的配置好像沒有問題。在網上搜尋到了原因:連結
按照裡面的方法把每個值都加上屬性即可。修改如下:
<?xml version="1.0" encoding="utf-8" ?><!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.session.Customer" table="CUSTOMER"> <id name="id" column="CID" type="java.lang.Integer"> <generator class="increment" /> </id> <property name="username" column="USERNAME" type="java.lang.String"/> <property name="password" column="PASSWORD" type="java.lang.String"/> </class></hibernate-mapping>
Java學習筆記--Hibernate架構錯誤集合及解決