【问题标题】:JPA - Entities are not stored in databaseJPA - 实体不存储在数据库中
【发布时间】:2014-02-24 17:58:19
【问题描述】:

当我尝试通过 JPA (@persistanceContex) 将数据插入数据库时​​遇到问题

观察

  1. 没有出现任何错误;
  2. 记录未存储到数据库中(保存)
  3. 当我尝试使用 listAll() 时;它从数据库中检索数据

@Entity
public class Test {

    @Id
    private int id;

    @Column(name="full_name")
    private String fullName;

    @Column(name="mobile_number")
    private int mobileNumber;

    .....

}

DAO 类

@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";
    }

应用程序上下文.xml

<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>

【问题讨论】:

  • hibernate是否创建了insert语句?,添加show_sql看看发生了什么
  • 我猜.. 你的ContextLoaderListenerDispatcherServlet 都有一个组件扫描并且都扫描相同的类。导致基本上没有交易。

标签: spring hibernate jpa annotations persistence


【解决方案1】:

首先,你不需要两个事务边界,我建议你从你的 DAO 中删除 @Transactional 并保留一个在你的服务中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多