当前位置: 首页 > 知识库问答 >
问题:

Spring JPA不存储实体

米丰
2023-03-14

我创建了一个基本的Spring MVC / JPA / Hibernate应用程序。我试图保存一个UserProfile实体来测试我是否真的可以持久化它,但是什么也没有保存,也没有抛出异常。

在控制器方法中,我创建了一个简单的UserProfile(这是一个@Entity),并将其发送到服务方法。类使用 @Service 对类进行批注,并使用 @Transactional 对 addUserProfile(UserProfile profile) 方法进行批注。

在服务方法中,我所做的就是调用一个DAO方法(用@Repository注释的类)。在DAO方法中,我所做的就是调用entitymanager . persist(object ), object是用户配置文件对象。

    < li >没有任何内容被写入服务器日志,日志级别为INFO。 < Li > Mysql查询日志中没有显示任何内容(我知道查询日志有效) < li >正确注入entityManager。 < li >数据源已正确启动,因为当我输入错误的凭据时,会出现SQLExceptions。

我希望你能告诉我出了什么问题。我会在下面发布我的一些代码和配置文件。

服务方法:

// The service method gets called from the controller. 
// Its class is annotated with @Service

@Transactional(readOnly = false)
public void addUserProfile(UserProfile userProfile) {
    userProfileDao.save(userProfile);
}

道法:

// The save(T object) method is in the GenericDaoJpa class, which is the superclass
// of the UserProfileDaoJPA class that is referenced from the service.
// I have established that the entityManager is there and the object is a
// UserProfile. The @Repository annotation is on the child class UserProfileDaoJpa.

public void save(T object) {
    entityManager.persist(object);
}

主应用程序-context.xml

<context:property-placeholder location="classpath*:**/*.properties"/>
<import resource="spring-jpa.xml"/>

application-context-web.xml文件

<mvc:annotation-driven />
<context:component-scan base-package="nl.codebasesoftware.produx" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

spring-jpa.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${db.driverClassName}"
      p:url="${db.url}" p:username="${db.username}" p:password="${db.password}"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/>

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>

persistence.xml

<persistence-unit name="mysqlPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

<!-- Needed to properly process @PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

不知何故,这个设置没有SQL发送到MySQL,但也没有抛出异常,所以我不知道发生了什么。希望你能帮忙:)

共有2个答案

公羊瀚
2023-03-14

好的,我知道了。一开始我以为我在这里找到了答案

在Spring中,声明性事务(@Transactional)不能与@Repository一起使用

但是经过更多的测试后,我发现这不是

但其内容。

删除 mode=“aspectj” 属性后,它开始工作!如果有人想评论为什么会这样,请这样做。

让我与你分享我的全部设置。这是针对带Hibernate 4的Spring 3.1的。注意:为了“简洁”,我只公布了配置文件的内容,并省略了外部标签< code >

web.xml内容

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<display-name>My Spring MVC web application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>produxDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:**/application-context-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>produxDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app> 

application-context.xml内容:

<!-- main setup -->
<context:property-placeholder location="classpath*:**/*.properties"/>
<context:annotation-config/>
<context:component-scan base-package="nl.codebasesoftware.produx.domain" />
<context:component-scan base-package="nl.codebasesoftware.produx.service" />
<context:component-scan base-package="nl.codebasesoftware.produx.dao" />
<!-- Data and JPA setup -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${db.driverClassName}"
      p:url="${db.url}" p:username="${db.username}" p:password="${db.password}"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven  transaction-manager="transactionManager"/>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>

application-context-web.xml的内容:

<mvc:annotation-driven />

<context:component-scan base-package="nl.codebasesoftware.produx.controller" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

webapps/META-INF/persistence.xml的内容

<persistence-unit name="mysqlPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/>  -->
    </properties>
</persistence-unit>

<!-- Needed to properly process @PersistenceContext which injects the entity manager -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

environment.properties的内容:

db.url=jdbc:mysql://localhost:3306/yourDatabaseName
db.driverClassName=com.mysql.jdbc.Driver
db.username=yourUsername
db.password=yourPassword

潘兴朝
2023-03-14

我认为你错过了服务中的事务传播。标记 readonly=false 只是将会话设置为自动刷新。但是设置适当的事务宣传将确保开始执行和提交/回滚。

在删除mode=“aspectj”后,它开始工作,因为我认为是因为,根据spring文档

默认模式“代理”将使用Spring的AOP框架处理要代理的带注释的bean(遵循代理语义学,如上所述,仅应用于通过代理传入的方法调用)。替代模式“aspectj”将改为使用Spring的AsheJ事务方面(修改目标类字节码以应用于任何类型的方法调用)编织受影响的类。AsheJ编织需要spring-aspects.jar类路径以及启用加载时编织(或编译时编织)。(有关如何设置加载时编织的详细信息,请参阅题为第6.8.4.5节“Spring配置”的部分。)

并且您可能还没有配置加载时织入

 类似资料:
  • 保存“我的实体”时,通过关系工作的子实体不会保存到它们的表中。我不明白怎么回事。 员工: EmployeePhoneNumber: 如何设置这些字段,然后保存实体: 在完成该方法后,我没有一个错误,所有的工作都是正确的,只是表格没有填满--为什么?

  • 我的应用程序中有一个Book model类,它如下所示: 返回NULL。为什么?

  • 我开始制作一个简单的spring boot应用程序。 我的第一步是利用Spring JDBC支持,使用默认的H2内存数据库。对于示例数据,我在src/main/resources中有schema.sql和data.sql。 所以当spring启动时,它也会执行这两个脚本并填充H2数据库,我可以通过H2控制台访问它。

  • 我在删除联接表中引用的实体时遇到问题。以下是三个链接的enitie。 当我尝试使用CrudRepository从来宾表中删除来宾时,它会给我这个错误。 错误:表“guest”上的更新或删除违反了表“guest\u group\u join”上的外键约束“FKKOUGVMCU860MOUACR1SHJXY”。键(id)=(4)仍然从表“guest\u group\u join”中引用。 有人能帮忙吗

  • 我在使用JPA时遇到了一些困难。我没有得到任何异常,但我不能保存任何东西到数据库。我从Hibernate到Jpa,在那里一切都工作得很好。下面是我的文件 Application.Properties: 存储库: 服务: 我在提交表单时得到了200的响应,但在数据库中找不到数据

  • 我目前正在尝试使用spring数据存储库删除我的一些实体。delete调用工作时没有任何异常/错误消息,但之后不会删除实体。 这些是我的实体: 而且 存储库非常简单: delete调用类似于 有什么想法为什么这个变化没有反映在数据库中吗? 编辑1: 我找到了变通办法,但我还是不明白真正的问题是什么。如果我像这样删除帖子(有几个例外,因为违反了约束,但帖子仍然会被删除),它就“起作用”了: 编辑2: