网站建设知识
Spring+Jersey+JPA+Hibernate+MySQL实现CRUD操作案例
2025-07-22 09:58  点击:0
Spring配置文件applicationContext中原先使用的是Hibernate,现在改为Hibernate对JPA的支持;增加了C3P0连接池;修改了Dao操作实现,改为Spring接管的JPA实现。 如果读者想详细查看Spring整合Jersey与前端交互可以点击上述连接。本文主要介绍以上三处修改内容,并且使用Jersey Test测试整合结果正确性。博文最后提供源码下载。至于JPA的介绍也不是本文的重点,若读者有不明白的地方可以查看其它文档。

一、添加pom依赖

因为整个项目构建是基于Maven的,所以在使用JPA和C3P0连接池之前,必须先添加相应的依赖JAR包,具体如下:

<!--{cke_protected}{C}%3C!%2D%2D%20hibernate%E5%AF%B9JPA%E6%94%AF%E6%8C%81%20%2D%2D%3E--><dependency><groupid>org.hibernate</groupid><artifactid>hibernate-entitymanager</artifactid><version>4.3.11.Final</version></dependency><!--{cke_protected}{C}%3C!%2D%2D%20hibernate%20C3P0%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%8C%85%20%2D%2D%3E--><dependency><groupid>org.hibernate</groupid><artifactid>hibernate-c3p0</artifactid><version>4.3.11.Final</version></dependency><dependency><groupid>com.mchange</groupid><artifactid>c3p0</artifactid><version>0.9.2.1</version></dependency><dependency><groupid>com.mchange</groupid><artifactid>mchange-commons-java</artifactid><version>0.2.3.4</version></dependency>

目前比较成熟的 JPA 框架主要包括 Jboss 的 Hibernate EntityManager、Oracle 捐献给 Eclipse 社区的 Eclipselink、Apache 的 OpenJPA 等,如上所示,本文使用的是Hibernate的JPA支持。

二、修改applicationContext.xml

正如Spring接管Hibernate一样,JAP也可以由Spring接管,只需要在applicationContext.xml文件做相应的配置即可,用户便无需关注诸如事务、安全等非业务逻辑的处理及实现。如下为修改后的Spring配置文件。

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E--><beans xmlns="springframework.org/schema/beans" xmlns:aop="springframework.org/schema/aop" xmlns:context="springframework.org/schema/context" xmlns:mvc="springframework.org/schema/mvc" xmlns:tx="springframework.org/schema/tx" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:schemalocation="    springframework.org/schema/beans     springframework.org/schema/beans/spring-beans-4.0.xsd    springframework.org/schema/tx    springframework.org/schema/tx/spring-tx-4.0.xsd    springframework.org/schema/aop    springframework.org/schema/aop/spring-aop-4.0.xsd    springframework.org/schema/context    springframework.org/schema/context/spring-context-4.0.xsd    springframework.org/schema/mvc     springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81%20%2D%2D%3E--><context:annotation-config><!--{cke_protected}{C}%3C!%2D%2D%20%E5%90%AF%E5%8A%A8%E7%BB%84%E4%BB%B6%E6%89%AB%E6%8F%8F%20%2D%2D%3E--><context:component-scan base-package="com.spring.jersy.jpa.hibernate.resource,com.spring.jersy.jpa.hibernate.dao,com.spring.jersy.jpa.hibernate.service"><!--{cke_protected}{C}%3C!%2D%2D%20JDBC%E5%B1%9E%E6%80%A7%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE%20%2D%2D%3E--><context:property-placeholder location="classpath:com/spring/jersy/jpa/hibernate/config/jdbc.properties"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%20%2D%2D%3E--><!--{cke_protected}{C}%3C!%2D%2D%20%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE%E8%BF%9E%E6%8E%A5%E6%B1%A0org.apachemons.dbcp.BasicDataSource%E5%88%99%E4%B8%8D%E7%AC%A6%E5%90%88%20%2D%2D%3E--><bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"><property name="driverClass" value="${jdbc.driverClassName}"><property name="jdbcUrl" value="${jdbc.url}"><property name="user" value="${jdbc.username}"><property name="password" value="${jdbc.password}"><!--{cke_protected}{C}%3C!%2D%2D%20hibernate.c3p0.min_size%3A%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E6%9C%80%E5%B0%8F%E8%BF%9E%E6%8E%A5%E6%95%B0%20%2D%2D%3E--><property name="minPoolSize" value="${jdbc.minPoolSize}"><!--{cke_protected}{C}%3C!%2D%2D%20hibernate.c3p0.max_size%3A%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BF%9E%E6%8E%A5%E6%95%B0%20%2D%2D%3E--><property name="maxPoolSize" value="${jdbc.maxPoolSize}"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%8C%87%E5%AE%9A%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96%E8%BF%9E%E6%8E%A5%E6%95%B0%20%20%E5%8F%96%E5%80%BC%E5%BA%94%E5%9C%A8minPoolSize%20%E4%B8%8E%20maxPoolSize%20%E4%B9%8B%E9%97%B4.Default%3A3%2D%2D%3E--> <property name="initialPoolSize" value="${jdbc.initialPoolSize}">  <!--{cke_protected}{C}%3C!%2D%2D%20%E7%BC%93%E5%AD%98%20Statement%20%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%95%B0%E9%87%8F%20%2D%2D%3E--><property name="maxStatements" value="${jdbc.maxStatements}"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E4%B8%AD%E8%BF%9E%E6%8E%A5%E5%AF%B9%E8%B1%A1%E5%9C%A8%E5%A4%9A%E9%95%BF%E6%97%B6%E9%97%B4%E6%B2%A1%E6%9C%89%E4%BD%BF%E7%94%A8%E8%BF%87%E5%90%8E%EF%BC%8C%E5%B0%B1%E5%BA%94%E8%AF%A5%E8%A2%AB%E9%94%80%E6%AF%81%20%2D%2D%3E--><property name="maxIdleTime" value="${jdbc.maxIdleTime}"><!--{cke_protected}{C}%3C!%2D%2D%20%E8%A1%A8%E7%A4%BA%E8%BF%9E%E6%8E%A5%E6%B1%A0%E6%A3%80%E6%B5%8B%E7%BA%BF%E7%A8%8B%E5%A4%9A%E9%95%BF%E6%97%B6%E9%97%B4%E6%A3%80%E6%B5%8B%E4%B8%80%E6%AC%A1%E6%B1%A0%E5%86%85%E7%9A%84%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%AF%B9%E8%B1%A1%E6%98%AF%E5%90%A6%E8%B6%85%E6%97%B6%20%2D%2D%3E--><property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"><!--{cke_protected}{C}%3C!%2D%2D%20%E5%BD%93%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E4%B8%AD%E7%9A%84%E8%BF%9E%E6%8E%A5%E8%80%97%E5%B0%BD%E6%97%B6%2C%20%E5%90%8C%E4%B8%80%E6%97%B6%E5%88%BB%E8%8E%B7%E5%8F%96%E5%A4%9A%E5%B0%91%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%20%2D%2D%3E--><property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property></property></property></property></property></property></property></property></property></property></property></bean><!--{cke_protected}{C}%3C!%2D%2D%20Hibernate%E5%AF%B9Jpa%E7%9A%84%E5%AE%9E%E7%8E%B0%20%2D%2D%3E--><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="hibernateJpaVendorAdapter"><!--{cke_protected}{C}%3C!%2D%2D%20Jpa%20%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%E5%99%A8%20%2D%2D%3E--><bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"></property></bean><!--{cke_protected}{C}%3C!%2D%2D%20%E5%AE%9A%E4%B9%89%E5%AE%9E%E4%BD%93%E7%AE%A1%E7%90%86%E5%99%A8%E5%B7%A5%E5%8E%82%20Jpa%E9%85%8D%E7%BD%AE%20LocalContainerEntityManagerFactoryBean%E8%BF%99%E4%B8%AA%E9%80%89%E9%A1%B9Spring%E6%89%AE%E6%BC%94%E4%BA%86%E5%AE%B9%E5%99%A8%E7%9A%84%E8%A7%92%E8%89%B2%E3%80%82%E5%AE%8C%E5%85%A8%E6%8E%8C%E7%AE%A1JPA%20%2D%2D%3E--><bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%8C%87%E5%AE%9A%E6%95%B0%E6%8D%AE%E6%BA%90%20%2D%2D%3E--><property name="dataSource" ref="dataSource"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%8C%87%E5%AE%9AJpa%E6%8C%81%E4%B9%85%E5%8C%96%E5%AE%9E%E7%8E%B0%E5%8E%82%E5%95%86%E7%B1%BB%2C%E8%BF%99%E9%87%8C%E4%BB%A5Hibernate%E4%B8%BA%E4%BE%8B%20%2D%2D%3E--><property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"><!--{cke_protected}{C}%3C!%2D%2D%20%E6%8C%87%E5%AE%9AEntity%E5%AE%9E%E4%BD%93%E7%B1%BB%E5%8C%85%E8%B7%AF%E5%BE%84%20%2D%2D%3E--><property name="packagesToScan"><array><value>com.spring.jersy.jpa.hibernate.model</value></array></property><!--{cke_protected}{C}%3C!%2D%2D%20%E6%8C%87%E5%AE%9AJPA%E5%B1%9E%E6%80%A7%EF%BC%9B%E5%A6%82Hibernate%E4%B8%AD%E6%8C%87%E5%AE%9A%E6%98%AF%E5%90%A6%E6%98%BE%E7%A4%BASQL%E7%9A%84%E6%98%AF%E5%90%A6%E6%98%BE%E7%A4%BA%E3%80%81%E6%96%B9%E8%A8%80%E7%AD%89%20%2D%2D%3E--><property name="jpaProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></property></property></bean><!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E4%BA%8B%E5%8A%A1%E7%89%B9%E6%80%A7%EF%BC%8C%E9%85%8D%E7%BD%AEadd%EF%BC%8Cdelete%EF%BC%8Cupdate%E5%BC%80%E5%A7%8B%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%8C%E4%BA%8B%E5%8A%A1%E4%BC%A0%E6%92%AD%E7%89%B9%E6%80%A7%E4%B8%BArequired%20%2D%2D%3E--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED"><tx:method name="delete*" propagation="REQUIRED"><tx:method name="update*" propagation="REQUIRED"><tx:method name="*" read-only="true"></tx:method></tx:method></tx:method></tx:method></tx:attributes></tx:advice><!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E9%82%A3%E4%BA%9B%E7%B1%BB%E7%9A%84%E6%96%B9%E6%B3%95%E8%BF%9B%E8%A1%8C%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%EF%BC%8C%E5%BD%93%E5%89%8D%3Cspan%20style%3D%22font-family%3A%20Arial%2C%20Helvetica%2C%20sans-serif%3B%22%3Ecom.spring.jersy.jpa.hibernate.service%3C%2Fspan%3E%E5%8C%85%E4%B8%AD%E7%9A%84%E5%AD%90%E5%8C%85%EF%BC%8C%20%E7%B1%BB%E4%B8%AD%E6%89%80%E6%9C%89%E6%96%B9%E6%B3%95%E9%9C%80%E8%A6%81%EF%BC%8C%E8%BF%98%E9%9C%80%E8%A6%81%E5%8F%82%E8%80%83tx%3Aadvice%E7%9A%84%E8%AE%BE%E7%BD%AE%20%2D%2D%3E--><aop:config><aop:pointcut expression="execution(* com.spring.jersy.jpa.hibernate.service.*.*(..))" id="allManagerMethod"><aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"></aop:advisor></aop:pointcut></aop:config></bean></context:property-placeholder></context:component-scan></context:annotation-config></beans>
jdbc.properties内容如下:
#MySQL jdbcjdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=root#c3p0jdbc.minPoolSize=5jdbc.maxPoolSize=20jdbc.initialPoolSize=15 jdbc.maxStatements=50jdbc.maxIdleTime=300jdbc.idleConnectionTestPeriod=60  jdbc.acquireIncrement=2
三、修改Dao实现

之前使用Hibernate作为持久层框架时,Spring提供了HibernateTemplate对数据库的CRUD等各种操作的实现。现在我们使用JPA操作时,Spring在配置文件中实现了EntityManagerFactory的注入实现,但是并没有提供EntityManager的实现。如果每次都要使用工厂类生成实体管理器,则非常不利于开发。JPA提供了@PersistenceContext注解才解决这个问题。如下:

package com.spring.jersy.jpa.hibernate.dao;import java.util.ArrayList;import java.util.List;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.Query;import org.springframework.stereotype.Repository;import com.spring.jersy.jpa.hibernate.model.User;@Repository(value = "userJpaDao")public class UserJpaDao {//通过该注解,在类中便无需EntityManagerFactory创建EntityManager了。@PersistenceContextprivate EntityManager entityManager;// 根据用户名和密码查询public List findByNameAndPassword(User user) {//如果sql语句中 select 有部分字段或者全部字段,返回的都是对象数组String jql = "select u " +" from User u where u.username=:username and u.password = :password";Query query = entityManager.createQuery(jql);query.setParameter("username", user.getUsername());query.setParameter("password", user.getPassword());@SuppressWarnings("unchecked")List listUsers = (List) query.getResultList();return listUsers;}//根据姓名查询@SuppressWarnings("unchecked")public List findUserByName(String name) {List userList = new ArrayList();String jql = "from User u where u.username like:name";Query query = entityManager.createQuery(jql);query.setParameter("name", name);userList = (List) query.getSingleResult();return userList;}//添加用户public boolean addUser(User user) {try {entityManager.persist(user);} catch (Exception e) {return false;}return true;}//删除用户public boolean deleteUser(Integer id) {try {User user = entityManager.find(User.class, id);if(user != null){entityManager.remove(user);return true;}else {return false;}} catch (Exception e) {return false;}}//修改用户public boolean updateUser(User user){try {String jql = "update User u set u.username = :name, u.password = :pwd, " +"u.address = :address, u.tel =:tel where u.id = :id";Query query = entityManager.createQuery(jql);query.setParameter("name", user.getUsername())     .setParameter("pwd", user.getPassword())     .setParameter("address", user.getAddress())     .setParameter("tel", user.getTel())     .setParameter("id", user.getId());    if(query.executeUpdate() == 1){    return true;    }else {    return false;    }} catch (Exception e) {return false;}}}
四、业务层实现

业务层实现不需要太多修改,只需引用JPA Dao实现即可。具体如下:

package com.spring.jersy.jpa.hibernate.service;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.spring.jersy.jpa.hibernate.dao.UserJpaDao;import com.spring.jersy.jpa.hibernate.model.User;@Service(value="userService")public class UserService {@Resource(name="userJpaDao")private UserJpaDao userJpaDao;public User findUserByNameAndPassword (User user) {List listUsers = userJpaDao.findByNameAndPassword(user);if(listUsers.size() > 0) {return listUsers.get(0);}return null;}public User findUserByName (String name) {        List listUsers = userJpaDao.findUserByName(name);if(listUsers.size() > 0) {return listUsers.get(0);}return null;}public boolean deleteUser(Integer id) {return userJpaDao.deleteUser(id);}public boolean addUser(User user) {return userJpaDao.addUser(user);}public boolean updateUser(User user){return userJpaDao.updateUser(user);}public UserJpaDao getUserJpaDao() {return userJpaDao;}public void setUserJpaDao(UserJpaDao userJpaDao) {this.userJpaDao = userJpaDao;}}
五、资源类

资源类实现主要是Jersey,本文因为上接Spring+Jersey+Hibernate+MySQL+HTML实现用户信息增删改查案例(附Jersey单元测试)文章,此处Jersey资源类不作任何修改,具体如下:

package com.spring.jersy.hibernate.resource;import javax.annotation.Resource;import javax.ws.rs.Consumes;import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.PUT;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import net.sf.json.JSONObject;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.spring.jersy.hibernate.model.User;import com.spring.jersy.hibernate.service.UserService;@Component@Path("/user")@Scope("prototype")public class UserResource {@Resource(name = "userService")private UserService userService;private String message;@GET@Path("/exist/{username}/{password}")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)//查询public String isExist(@PathParam("username") String username,@PathParam("password") String password) {User user = new User();user.setUsername(username);user.setPassword(password);User result = userService.findUserByNameAndPassword(user);JSonObject jsonUser = JSONObject.fromObject(result);return jsonUser.toString();}@POST@Path("/addUser")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)//添加public String addUser(User user) {boolean flag = userService.addUser(user);if (flag) {message = "success";} else {message = "fail";}JSonObject jsonObject = new JSonObject();jsonObject.put("message", message);return jsonObject.toString();}@DELETE@Path("/deleteUser/{id}")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)//删除public String deleteUser(@PathParam("id") Integer id) {boolean flag = userService.deleteUser(id);if (flag) {message = "success";} else {message = "fail";}JSonObject jsonObject = new JSonObject();jsonObject.put("message", message);return jsonObject.toString();}@PUT@Path("/updateUser/{id}")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)//修改public String updateUser(@PathParam("id") Integer id, User user) {user.setId(id);boolean flag = userService.updateUser(user);if (flag) {message = "success";} else {message = "fail";}JSonObject jsonObject = new JSonObject();jsonObject.put("message", message);return jsonObject.toString();}public void setUserService(UserService userService) {this.userService = userService;}}
六、单元测试

此处使用的单元测试是Jersey Test,测试类内容如下:

package com.spring.jersy.hibernate.test;import org.springframework.web.context.ContextLoaderListener;import org.springframework.web.context.request.RequestContextListener;import com.sun.jersey.spi.spring.container.servlet.SpringServlet;import com.sun.jersey.test.framework.JerseyTest;import com.sun.jersey.test.framework.WebAppDescriptor;public abstract class baseJerseyServiceTest extends JerseyTest {    @Override    protected WebAppDescriptor configure() {        return  new WebAppDescriptor.Builder("com.spring.jersy.jpa.hibernate.resource")        .contextParam( "contextConfigLocation", "classpath:com/spring/jersy/hibernate/config/applicationContext.xml")        //.servletClass(SpringServlet.class)        .filterClass(SpringServlet.class)        .initParam("com.sun.jersey.config.feature.Redirect", "true")        .initParam("com.sun.jersey.config.feature.FilterForwardOn404", "true")        .initParam("com.sun.jersey.config.property.WebPageContentRegex", "/(images|css|jsp)/.*")        .initParam("com.sun.jersey.api.json.POJOMappingFeature", "true")        .initParam("com.sun.jersey.config.property.packages", "com.spring.jersy.jpa.hibernate.resource")        .contextListenerClass(ContextLoaderListener.class)        .requestListenerClass(RequestContextListener.class)        .build();    }}
package com.spring.jersy.jpa.hibernate.test;import javax.ws.rs.core.MediaType;import org.junit.Before;import org.junit.Test;import com.spring.jersy.jpa.hibernate.model.User;import com.sun.jersey.api.client.WebResource;public class UserResourceTest extends baseJerseyServiceTest {private WebResource webResource;@Beforepublic void setUp() throws Exception {webResource = resource();// webResource.accept("application/json");}@Testpublic void testIsExist() {String response = webResource.path("/user/exist/xx/1233").get(String.class);System.out.println(response);}@Testpublic void testAddUser() {User user = new User();user.setUsername("wwwx");user.setPassword("pwd");user.setAddress("江苏盐城");user.setTel("12333");String response = webResource.path("/user/addUser").entity(user, "application/json").post(String.class, user);System.out.println(response.toString());}@Testpublic void testDeleteUser() {String response = webResource.path("/user/deleteUser/59").delete(String.class);System.out.println(response.toString());}@Testpublic void testUpdateUser() {User user = new User();user.setUsername("王三儿");user.setPassword("wangsaner");String response = webResource.path("/user/updateUser/64").entity(user, MediaType.APPLICATION_JSON).put(String.class);System.out.println(response.toString());}}
七、测试结果如下:
Hibernate:     insert     into        test.user        (username, password, address, tel)     values        (?, ?, ?, ?){"message":"success"}Hibernate:     select        user0_.id as id1_2_0_,        user0_.username as username2_2_0_,        user0_.password as password3_2_0_,        user0_.address as address4_2_0_,        user0_.tel as tel5_2_0_     from        test.user user0_     where        user0_.id=?{"message":"fail"}Hibernate:     update        test.user     set        username=?,        password=?,        address=?,        tel=?     where        id=?Hibernate:     select        user0_.id as id1_2_,        user0_.username as username2_2_,        user0_.password as password3_2_,        user0_.address as address4_2_,        user0_.tel as tel5_2_     from        test.user user0_     where        user0_.username=?         and user0_.password=?{"address":"ss","id":48,"password":"1233","tel":"1232333","username":"xx"}


附1:异常

将项目部署到服务器上运行,在Web调试过程中,所有调试一切正常,但是在运行单元测试的时候报如下异常错误:

Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:973)at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824)at org.hibernate.cfg.Configuration$metadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)at org.hibernate.cfg.Configuration$metadataSourceQueue.processmetadata(Configuration.java:3799)at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)... 45 more
解决办法:具体原因是因为项目中存在其他版本的JPA实现,导致版本冲突,在调用javax.persistence.*时冲突,如Eclipse link,所以需要在pom.xml中去掉Eclipse 对JPA的支持。