成功在Netbeans下配置hibernate并…

来源:互联网 发布:淘宝怎么做数据分析 编辑:程序博客网 时间:2024/05/17 23:26
The following 1 connections were not migrated since identicalconnections already existed in the Workbench connectionsfile:

   * Local instanceMySQLlzl

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格式、
 <?xmlversion="1.0"encoding="UTF-8"?>之前也不允许有空格,必须是XML文件的第一个元素。
参考资料:http://blog.csdn.net/stc89cxx/article/details/50525322


按照教程编写程序后,比较坑爹的地方:
一、org.hibernate.MappingException: invalid configuration
谢谢亲,已经解决了,是我的配置文件的头写错了。。
以前的头:
<?xmlversion="1.0"encoding="utf-8"?>
<hibernate-configuration
      xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
      xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configurationhibernate-configuration-4.0.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
修改后的头:
<?xmlversion='1.0'encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/HibernateConfiguration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

改成这个样子就可以了。。
by:http://bbs.csdn.net/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.csdn.net/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 existsnewschema.stu_tab

    create tablenewschema.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 connectionwithout server's identity verification is notrecommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+requirements SSL connection must be established by default ifexplicit option isn't set. For compliance withexisting applications not using SSL the verifyServerCertificateproperty is set to 'false'. Youneed either to explicitly disable SSL by setting useSSL=false, orset useSSL=true and provide truststore for server certificateverification.
【解决办法】在地址后面加?useSSL=true
【更改后】
第二个WARN。
WARN: HHH000223: Recognized obsolete hibernate namespacehttp://hibernate.sourceforge.net/. Use namespacehttp://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6Migration Guide!
【更改前】
<?xmlversion='1.0'encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/HibernateConfiguration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
【更改后】
<?xmlversion='1.0'encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/HibernateConfiguration 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.DriverManagerConnectionProviderImplconfigure
WARN: HHH000402: Using Hibernate built-in connection pool (notfor production use!)
答案当然是配置自己的连接池,添加下面的代码到hibernate.cfg.xml文件里面:
    <propertyname="c3p0.min_size">5</property><!--在连接池中可用数据库连接的最小数目-->
    <propertyname="c3p0.max_size">30</property><!--在连接池中所有数据库连接的最大数目-->
    <propertyname="c3p0.time_out">1800</property><!--设定数据库连接的超时时间-->
    <propertyname="c3p0.max_statement">50</property><!--可以被缓存的PreparedStatement的最大数目-->
终于得到一个没有error的文件运行了。更详细的连接池配置问题:
http://blog.csdn.net/pengpegv5yaya/article/details/23359787



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


另外,附一些资料:
《hibernate 里面 mysql dialect 配置》
http://blog.csdn.net/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

0 0