Spring JPA-实体不存储在数据库中

Spring JPA-实体不存储在数据库中,spring,hibernate,jpa,annotations,persistence,Spring,Hibernate,Jpa,Annotations,Persistence,当我试图通过JPA(@persistanceContex)将数据插入数据库时,我遇到了一个问题 观察结果 没有任何错误 记录未存储到数据库中(保存) 当我尝试使用listAll()时;它从数据库中检索数据 领域 刀类 服务 控制器 application-context.xml 首先,您不需要两个事务边界,我建议您从DAO中删除@Transactional,并将其保留在您的服务中 首先验证spring事务是否已启动事务:使用调试器并在事务边界之后停止应用程序,例如在TestService.sav

当我试图通过JPA(@persistanceContex)将数据插入数据库时,我遇到了一个问题

观察结果

  • 没有任何错误
  • 记录未存储到数据库中(保存)
  • 当我尝试使用listAll()时;它从数据库中检索数据
  • 领域 刀类 服务 控制器 application-context.xml
    首先,您不需要两个事务边界,我建议您从DAO中删除
    @Transactional
    ,并将其保留在您的服务中


    首先验证spring事务是否已启动事务:使用调试器并在事务边界之后停止应用程序,例如在
    TestService.save
    -方法中。如果事务正在运行,您将在调用堆栈中看到
    org.springframework.transaction.interceptor.TransactionInterceptor#invoke
    。如果您没有看到TransactionInterceptor,那么这就是您的问题。如果事务正在运行,请发布您的
    persistence.xml
    文件。

    是否在insert语句中创建了hibernate?添加show\u sql以查看发生了什么我猜。。您的
    ContextLoaderListener
    DispatcherServlet
    都有组件扫描,并且都扫描相同的类。导致基本上没有交易。
    @Entity
    public class Test {
    
        @Id
        private int id;
    
        @Column(name="full_name")
        private String fullName;
    
        @Column(name="mobile_number")
        private int mobileNumber;
    
        .....
    
    }
    
    @Repository("testDAO")
    @Transactional
    public class TestDAO {
    
    
        private EntityManager entityManager;
    
         @PersistenceContext(unitName="CRUD_Test_Annotation")
        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }
    
        public void save(Test test){
            entityManager.persist(test);
        }
    }
    
    @Service("testService")
    @Transactional
    public class TestService {
    
        private static final Logger logger = LoggerFactory.getLogger(TestService.class);
    
        @Autowired(required=true)
        private TestDAO testDAO;
    
        public void save(Test test){
            logger.info("TestService::save()");
            testDAO.save(test);
        }
    
        public void list(){
            testDAO.getAll();
        }
    
    }
    
    @RequestMapping(value = "/add", method = RequestMethod.GET)
        public String add(Locale locale, Model model) {
    
            Test test = new Test();
            test.setId(xx);
            test.setFullName("xxxxx");
            test.setMobileNumber(yyyyyy);
    
            testService.save(test);
            return "home";
        }
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    
        <!-- Declare a JPA entityManagerFactory-->
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    
            <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
            <property name="persistenceUnitName" value="CRUD_Test_Annotation" />
    
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true" />
                </bean>
            </property>
        </bean>
    
        <!-- Declare a transaction manager-->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>