成功在Netbeans下配置hibernate并…

本文详细记录了在Netbeans环境下配置Hibernate的过程及遇到的问题,包括XML配置错误、缺失库文件、JDBC驱动问题和SSL连接警告等,并提供了相应的解决方案和参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The following 1 connections were not migrated since identical connections already existed in the Workbench connections file:

   * Local instance MySQLlzl

Connection passwords were migrated successfully.

主要教程,参考这里:http://wenku.baidu.com/view/09dd02ca7cd184254a35358b.html
编程环境:Netbeans with JDK
下载的hibernate版本:hibernate-release-5.2.4.Final
足足有253.929KB
然而实际使用却用了NetBeans自带的hibernate库,版本是4点x

首先遇到的问题是:hibernate.cfg.xml not found
显然没放对位置。这里得了解一下Netbeans的文件结构,由于用的中文系统,所以src被翻译成了“源包”。果断把文件复制到其下。
然而,然而的然而:xml文件并不能用
规范的XML格式、
 <?xml version="1.0" encoding="UTF-8"?>之前也不允许有空格,必须是XML文件的第一个元素。
参考资料:http://blog.youkuaiyun.com/stc89cxx/article/details/50525322


按照教程编写程序后,比较坑爹的地方:
一、org.hibernate.MappingException: invalid configuration
谢谢亲,已经解决了,是我的配置文件的头写错了。。
以前的头:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
修改后的头:
<?xml version='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">

改成这个样子就可以了。。
by:http://bbs.youkuaiyun.com/topics/390981649
也可以看简洁版本:http://www.myexception.cn/java-web/1847092.html





(没有出错,但是看到的解决方案:http://blog.sina.com.cn/s/blog_a59efe3c01015hff.html)



二、java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
在使用Hibernate3的时候,发现程序编译好了,在运行时总是抛出java.lang.NoClassDefFoundError: javax/persistence/EntityListeners异常,经查找是因为缺少ejb3-persistence.jar包。 
只需要在类库中加入ejb3-persistence.jar,就不会出现这个异常了。 
这里不理解的是Hibernate3, 为什么不把ejb3-persistence.jar直接放到自己需要的类库中,而非让用户自己找,去添加。
我是自己上网搜了一个。在hibernate的解压目录下没有找到这个文件。


三、Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
解决办法:http://blog.youkuaiyun.com/you23hai45/article/details/37729257
缺少hibernate必备的一个jar包,“hibernate-jpa-2.1-api-1.0.0.Final.jar”
我Jar的在解压后的hibernate文件夹中的\hibernate-release-5.2.4.Final\lib\required里面




三、
文件缺这缺那的
最后发觉JDBC是connector的驱动


四、文件能成功运行了,输出也正常:
    drop table if exists newschema.stu_tab

    create table newschema.stu_tab (
        stu_id integer not null auto_increment,
        stu_name varchar(255),
        stu_age integer,
        primary key (stu_id)
    )
但是进workbench一看,没有任何数据库变动,该创建的表也没有创建
原来需要配置schema:
hibernate schema 设置:http://rking0228.iteye.com/blog/777486


细节方面的修补,处理WARN:
Wed Nov 30 16:22:44 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
【解决办法】在地址后面加?useSSL=true
【更改后】
第二个WARN。
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
【更改前】
<?xml version='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">
【更改后】
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

第三个WARN
十一月 30, 2016 4:30:32 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
答案当然是配置自己的连接池,添加下面的代码到hibernate.cfg.xml文件里面:
    <property name="c3p0.min_size">5</property> <!--在连接池中可用数据库连接的最小数目-->
    <property name="c3p0.max_size">30</property> <!--在连接池中所有数据库连接的最大数目-->
    <property name="c3p0.time_out">1800</property> <!--设定数据库连接的超时时间-->
    <property name="c3p0.max_statement">50</property> <!--可以被缓存的PreparedStatement的最大数目-->
终于得到一个没有error的文件运行了。更详细的连接池配置问题:
http://blog.youkuaiyun.com/pengpegv5yaya/article/details/23359787



<!-- format SQL in log and console --> <property name="hibernate.format_sql">true</property>


另外,附一些资料:
《hibernate 里面 mysql dialect 配置》
http://blog.youkuaiyun.com/maxiao1204/article/details/52317197
其他hibernate开发教程:
http://blog.chinaunix.net/uid-26284395-id-3048988.html
Odata相关,如何将hibernate转化为Odata
http://stackoverflow.com/questions/16990252/how-to-expose-a-mysql-database-as-odata

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值