Spring MVC+Hibernate JPA搭建的博客系统项目中所遇到的坑

标签: springmvc hibernate

2016年12月21日 21:48:035133人阅读 评论(0) 收藏 举报

 分类:

Spring/Spring MVC(6)  Hibernate(1) 

版权声明:本文为博主原创文章,未经博主允许不得转载,如需转载,请注明文章出处为 http://www.54tianzhisheng.cn/,否则考虑法律追究责任,谢谢合作! https://blog.csdn.net/tzs_1041218129/article/details/53791765

目录(?)[+]

项目代码地址:https://github.com/zhisheng17/springmvc

如果觉得不错的话,欢迎给个 star , 如果你想完善这个项目的话,你也可以 fork 后修改然后推送给我。

最近在学习 Spring MVC ,其中在做一个简单的博客系统demo,是使用 SpringMVC 集成

Spring Data JPA(由 Hibernate JPA 提供),来进行强大的数据库访问。结果其中遇到的坑不

是一点点啊,我差点崩溃了,其中最大的原因就是由于 Hibernate JPA 中的bug了,反正一开始

还不知道是这个问题,导致折腾了快一天的时间。想想都可怕啊。

mvc-dispatch-servlet.xml代码


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--指明 controller 所在包,并扫描其中的注解-->
    <context:component-scan base-package="cn.zhisheng.controller"/>

    <!-- 静态资源(js、image等)的访问 -->
    <mvc:default-servlet-handler/>

    <!-- 开启注解 -->
    <mvc:annotation-driven/>

    <!--ViewResolver 视图解析器-->
    <!--用于支持Servlet、JSP视图解析-->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 表示JPA Repository所在的包 -->
    <jpa:repositories base-package="cn.zhisheng.repository"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="defaultPersistenceUnit"/>
        <property name="packagesToScan" value="cn.zhisheng.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/springdemo?useSSL=false</prop>
                <prop key="hibernate.connection.username">root</prop>
                <prop key="hibernate.connection.password">root</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autoReconnect">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="connection.autoReconnectForPools">true</prop>
                <prop key="connection.is-connection-validation-required">true</prop>

                <prop key="hibernate.c3p0.validate">true</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">600</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.preferredTestQuery">SELECT 1;</prop>
                <prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
    </bean>

    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- 开启事务管理注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.zhisheng</groupId>
  <artifactId>springmvc</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <spring.version>4.2.6.RELEASE</spring.version>
    <hibernate.version>5.1.0.Final</hibernate.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>1.10.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.39</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

一开始我是用默认的在resources文件里面生成了persistence.xml配置文件进行数据库配置的,后来由于用那种方法,碰到的问题有很多,自己搞了好几个个小时都没弄好,只好换种方法,没想到竟然还是这种效果(泪崩),看来是不治标也不治本。

无奈,只好硬刚了,碰到错误,百度+google,看了大量的的解决方法,都是没用,慢慢的我所加的jar包越来越多,用maven管理的依赖的也变得多起来了,但终究是不能够解决问题的。

其实这时我看了这么多的博客和解决方法,我已经知道了是 Hibernate JPA 的bug问题,途中自己也换了一些版本,还是没能解决办法。

最后在吃完完晚饭后,又折腾了快三小时,终于找到可靠有用的解决方案了。

运行成功后,我当时就激动起来了。马丹,老子终于将你解决了。

所以在这里立马就将自己这次的血崩历史纪录下来。

下面写下遇到的问题:(其中有些可能还不记得写了)

  • java.lang.ClassNotFoundException: javax.persistence.EntityManager
  • java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
  • javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit
  • javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersi
  • java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
  • java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
  • java.lang.ClassNotFoundException: org.hibernate.MappingException
  • NoSuchMethodError: javax.persistence.xxx

等,还有几个,忘记了。。

首先通过报错信息可以知道有些是因为jar包的问题,但是并不是光是缺少jar包的问题,很大的原

因就是因为jar包的版本不同,刚好那个jar包又是有问题的(自身有bug)。

就比如错误:

java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;

就是因为JAVAEE6.0中的 javax.persistence.jar与 hibernate4.3.8中的hibernate-jpa-2.1-api-1.0.0.Final.jar冲突

JoinColumn.foreignKey() was introduced with JPA 2.1, which was not implemented by Hibernate 4 until version 4.3. If you’re using an older version of Hibernate 4 then try upgrading to 4.3.x.

If you’re already using Hibernate 4.3 then make sure you’re also using JPA 2.1 to make sure the API and implementation match up.

图片来自 : http://stackoverflow.com/questions/24588860/error-javax-persistence-joincolumn-foreignkeyljavax-persistence-foreignkey-wi

I finally solved this similar problem, there was an old version(hibernate-jpa-2.0-api-1.0.0-Final.jar) in my lib folder which I guess has been preventing maven dependency from loading.

So after I manually deleted it and added (hibernate-jpa-2.1-api-1.0.0-Final.jar) everything started to work.

意思大概就是:

因为JAVAEE6.0中的 javax.persistence.jar与 hibernate4.3.8中的hibernate-jpa-2.1-api-1.0.0.Final.jar冲突 ,我们在pom文件下添加依赖后,竟然没发现在 springmvc(项目名称)\target\springmvc(项目名称)\WEB-INF\lib 下看到 javax.persistence.jar 文件,结果竟然在 springmvc\lib下找到他了。

解决办法就是在 pom文件和 mvc-dispatcher-servlet.xml 都配置好的情况下,将 springmvc\lib下的 javax.persistence.jar 删除。

最后再说一句:Though the error drove almost crazy, hold on, you wil get smile ! Fighting

原文地址:https://www.cnblogs.com/iOS-mt/p/8889599.html

时间: 2024-08-25 17:37:21

Spring MVC+Hibernate JPA搭建的博客系统项目中所遇到的坑的相关文章

博客系统项目搭建准备

一.项目背景 1999年初,由于 Cameron与Jesse共同维护的博客站点列表既有趣又易于阅读,吸引了很多人的眼球.在这种情况下,Peter Merholz宣称:"这个新鲜事物必将引起大多数人的注意.作为未来的一个常用词语,web-blog将不可避免地被简称为blog,而那些编写网络日志的人,也就顺理成章地成为blogger"博客".这代表着博客被正式命名. 8月份,Pyra发布了Blogger网站,Groksoup也投入运营,使用这些企业所提供的简单的基于互联网的工具,

博客系统项目搭建

一.maven工程的好处 1.一步构建 maven对项目构建的过程进行标准化,通过一个命令即可完成构建过程. 2.依赖管理 maven为全世界的java开发者提供了一个免费的中央仓库,在其中几乎可以找到任何的流行开源软件.通过衍生工具,我们还能对其进行快速搜索,因此maven工程不用手动导入jar包,可以通过在pom.xml中定义坐标从中央仓库自动下载,方便并且不容易出错. 3.maven的跨平台,可在window.linux上使用. 4.maven遵循规范开发有利于提高大型团队的开发效率,降低

巨杉Tech | 十分钟快速搭建 Wordpress 博客系统

介绍很多互联网应用程序开发人员第一个接触到的网站项目就是博客系统.而全球使用最广的Wordpress常常被用户用来快速搭建个人博客网站.默认情况下,Wordpress一般在后台使用MySQL关系型数据库存储所有的博文及回复.本文将展示如何使用 SequoiaDB 巨杉分布式数据库替换MySQL,成为Wordpress博客系统的后台关系型数据库. 通过阅读本文,用户可以了解到如何使用SequoiaDB巨杉数据库的MySQL实例无缝替换标准MySQL数据库.SequoiaDB巨杉数据库允许用户在不更

Spring+Spring MVC+Hibernate框架搭建实例

前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: DROP TABLE IF EXISTS `users`; CREATE TABLE `users` (   `UserID` int(4) NOT NULL AUTO_INCREMENT,   `UserName` varchar(16) NOT NULL,   `Password` varchar(16) NOT NULL,   `Telep

SpringBoot技术栈搭建个人博客【项目准备】

前言:很早之前就想要写一个自己的博客了,趁着现在学校安排的实习有很多的空档,决定把它给做出来,也顺便完成实习的任务(搞一个项目出来...) 需求分析 总体目标:设计一套自适应/简洁/美观/易于文章管理发布的一个属于我个人的博客,最后一页能展示我个人的简历,因为大三快结束了马上就该去找工作了...哦忘了,最重要的还是要支持Markdown才行,因为已经习惯了... 前端需求分析 首先,前端的页面要求是: ①简洁/美观--个人很喜欢像Mac那样的简洁风,越简单越好,当然也得好看: ②最好是单页面--

怎么让wordpress用sqlite3 搭建轻量级博客系统

wordpress 默认是用mysql作为数据库支持,这个对个人站长来说还是有点麻烦了些.特别是如果以后网站备份迁移就有点事多了. 之前用django开发自己的博客感觉其实用sqlite3作为数据库插好,就是一个文件而已.备份网站,直接打包整个目录即可方便省事. 那么作为个人站长,如果要用wordpress和sqlite3来建设网站的话怎么搞呢?这里在windows环境我试了一下,可行方便.如果是生产环境,请自要百度linux安装wordpress教程. 1.准备工作 1.自行搭建php运行环境

两天的成果---node,express,mongodb,以及mongoose 对github搭建自己博客的项目进行分析

github地址:https://github.com/linguowei/myblog 把项目git clone下来: 分析: # git clone https://github.com/linguowei/myblog.git # cd myblog # npm install # npm run build # cd admin # npm run build #. cd ../ # node app.js # localhost:7000 # localhost:7000/admin

lnmp 搭建 typecho博客系统

yum安装 lnmp (linux+nginx+php7.1+mysql5.7)1.第一步先更新yum update只适合新服务器,生成环境慎用2.yum安装nginx安装nginx最新源:yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmyum repolist enabled | grep "nginx*"安装nginx

搭建基于全注解的Spring+Spring MVC+Hibernate框架

以实例讲解Spring+Spring MVC+Hibernate框架搭建步骤: 一.配置web.xml Xml代码   <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XML