• Hibernate学习笔记--Hibernate框架错误集合及解决


    错误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 JNDI
    javax.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>
  • 相关阅读:
    Python3 list基本操作
    Oracle Sql关于case-when,if-then,decode
    Oracle12c解锁scott测试用户
    Java得到下一天日期
    SQLiteTest源代码
    实况照片的视频合并
    一个支持中文的日志类
    为对话框添加背景图片
    获取当前应用程序的文件名
    Win7系统x64正在准备再循环
  • 原文地址:https://www.cnblogs.com/gnivor/p/4325975.html
Copyright © 2020-2023  润新知