При добавлении объекта из класса, реализованного службой, в интерфейс репозитория отображается успешное сообщение, но данные не вставляются в базу данных. Я вызываю метод save(entity) из интерфейса JpaRepository. вместо этого он показывает сообщение об успешном вставке записи.
Контроллер --- >>.
@Controller
@RequestMapping("/cart")
public class CartController {
@Autowired
CartService cartServices;
/**
* Get All records in Cart
*
* @return All Cart List
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody List getCart() {
List cartList = null;
try {
cartList = cartServices.getCartList();
if(cartList.isEmpty())
{
System.out.println("List is empty ");
}
} catch (Exception e) {
e.printStackTrace();
}
return cartList;
}
/**
* Add Cart
*
* @param representative
* @return Status
*/
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status addCart(@RequestBody Cart cart) {
System.out.println("i m from jpa controller");
try {
cartServices.save(cart);
return new Status(1, "Cart added Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
}
}
Класс «Реализованный сервис» ------>>>>
@Service
public class CartServiceImpl implements CartService {
@Inject
CartRepository cartRepository;
/**
* Get All Cart
*
* @throws Exception
* @return All Cart List
*/
@Override
@Transactional
public List getCartList() throws Exception {
// TODO Auto-generated method stub
return cartRepository.findAll();
}
/**
* Add Cart
*
* @param cart
* @throws Exception
* @return Boolean
*/
@Override
@Transactional
public Cart save(Cart cart) throws Exception {
System.out.println("i m from jpa Service");
Cart cart1 = cartRepository.save(cart);
return cart1;
}
Интерфейс репозитория -------->>>>
package com.xptraining.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.xptraining.model.Cart;
@Repository
public interface CartRepository extends JpaRepository
{
}
Web.xml
xptraining
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/rest.xml
1
mvc-dispatcher
/
rest.xml
org.hibernate.dialect.MySQL5Dialect
${hibernate.show_sql}
update
com.xptraining.model.Product
com.xptraining.model.Specialties
com.xptraining.model.Representative
com.xptraining.model.Cart
com.xptraining.model.Sku
com.xptraining.model.Order
org.hibernate.dialect.MySQL5Dialect
${hibernate.show_sql}
@Моя консоль, когда я вызываю метод сохранения
10:48:55.219 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing POST request for [/xptraining/cart/create]
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /cart/create
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public com.xptraining.model.Status com.xptraining.controller.CartController.addCart(com.xptraining.model.Cart)]
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'cartController'
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read [class com.xptraining.model.Cart] as "application/json" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@48e069ed]
i m from jpa controller
i m from jpa Service
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@23b8bcb1 updates=org.hibernate.engine.spi.ExecutableList@2d7f56b6 deletions=org.hibernate.engine.spi.ExecutableList@2940d7c1 orphanRemovals=org.hibernate.engine.spi.ExecutableList@6c425c8d collectionCreations=org.hibernate.engine.spi.ExecutableList@a793880 collectionRemovals=org.hibernate.engine.spi.ExecutableList@5d64159f collectionUpdates=org.hibernate.engine.spi.ExecutableList@72b52f2d collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@c6da3c2 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@23b8bcb1 updates=org.hibernate.engine.spi.ExecutableList@2d7f56b6 deletions=org.hibernate.engine.spi.ExecutableList@2940d7c1 orphanRemovals=org.hibernate.engine.spi.ExecutableList@6c425c8d collectionCreations=org.hibernate.engine.spi.ExecutableList@a793880 collectionRemovals=org.hibernate.engine.spi.ExecutableList@5d64159f collectionUpdates=org.hibernate.engine.spi.ExecutableList@72b52f2d collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@c6da3c2 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtaining JDBC connection
10:48:55.220 [http-nio-8080-exec-10] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/training]
10:48:55.223 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtained JDBC connection
10:48:55.223 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - begin
10:48:55.223 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - initial autocommit status: true
10:48:55.223 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - disabling autocommit
10:48:55.223 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Exposing Hibernate transaction as JDBC transaction [com.mysql.jdbc.JDBC4Connection@5c26bcfd]
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Opening JPA EntityManager
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.hibernate.loader.Loader - Loading entity: [com.xptraining.model.Cart#1]
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.hibernate.SQL - select cart0_.cart_id as cart_id1_0_0_, cart0_.prize as prize2_0_0_, cart0_.quantity as quantity3_0_0_, cart0_.representative_id as represen4_0_0_, cart0_.sku_id as sku_id5_0_0_ from cart cart0_ where cart0_.cart_id=?
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtaining JDBC connection
10:48:55.224 [http-nio-8080-exec-10] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/training]
10:48:55.226 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Obtained JDBC connection
10:48:55.226 [http-nio-8080-exec-10] DEBUG org.hibernate.loader.Loader - Result set row: 0
10:48:55.226 [http-nio-8080-exec-10] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[com.xptraining.model.Cart#1]
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Resolving associations for [com.xptraining.model.Cart#1]
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done materializing entity [com.xptraining.model.Cart#1]
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.loader.Loader - Done entity load
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Releasing JDBC connection
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Released JDBC connection
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Initiating transaction commit
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Committing Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@23b8bcb1 updates=org.hibernate.engine.spi.ExecutableList@2d7f56b6 deletions=org.hibernate.engine.spi.ExecutableList@2940d7c1 orphanRemovals=org.hibernate.engine.spi.ExecutableList@6c425c8d collectionCreations=org.hibernate.engine.spi.ExecutableList@a793880 collectionRemovals=org.hibernate.engine.spi.ExecutableList@5d64159f collectionUpdates=org.hibernate.engine.spi.ExecutableList@72b52f2d collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@c6da3c2 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - committing
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - committed JDBC Connection
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - re-enabling autocommit
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@23b8bcb1 updates=org.hibernate.engine.spi.ExecutableList@2d7f56b6 deletions=org.hibernate.engine.spi.ExecutableList@2940d7c1 orphanRemovals=org.hibernate.engine.spi.ExecutableList@6c425c8d collectionCreations=org.hibernate.engine.spi.ExecutableList@a793880 collectionRemovals=org.hibernate.engine.spi.ExecutableList@5d64159f collectionUpdates=org.hibernate.engine.spi.ExecutableList@72b52f2d collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@c6da3c2 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Releasing JDBC connection
10:48:55.227 [http-nio-8080-exec-10] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Released JDBC connection
10:48:55.228 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Written [com.xptraining.model.Status@788e053b] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@48e069ed]
10:48:55.228 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
10:48:55.228 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
Подробнее здесь: https://stackoverflow.com/questions/427 ... repository
Как сохранить данные с помощью метода сохранения репозитория jpa ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Собственный SQL-запрос UPSERT для сбора как @Param метода репозитория JPA
Anonymous » » в форуме JAVA - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Возврат метода save() репозитория JPA возвращает классы композиции как null [закрыто]
Anonymous » » в форуме JAVA - 0 Ответы
- 41 Просмотры
-
Последнее сообщение Anonymous
-