Spring data Jpa实体不受管理调用刷新时出现异常

2024-05-08

我有一个数据库代码 jar,我在不同的应用程序中使用它来访问数据库。我在用spring data jpa。我需要调用刷新来检查来自其他应用程序的数据库行的更改。这里我如何实现它。 我的StudentRepository界面:

public interface StudentRepository extends JpaRepository<StudentEntity, Integer>, StudentRepositoryCustom {}

My StudentRespositoryCustom界面

public interface StudentRepositoryCustom {
    void detach(StudentEntity studentEntity);
    void refresh(StudentEntity studentEntity);}

My StudentRepositoryCustomImpl class

public class StudentRepositoryImpl implements StudentRepositoryCustom {

@PersistenceContext
EntityManager entityManager;

@Override
@Transactional
public void detach(StudentEntity studentEntity) {
    entityManager.detach(studentEntity);
}

@Override
@Transactional
public void refresh(StudentEntity studentEntity) {
    entityManager.refresh(studentEntity);
}}

My Test

@Test
public void refreshTest(){
    StudentEntity studentEntity = studentRepository.findOne(6);
    studentRepository.refresh(studentEntity);
}

但它抛出了这个异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Entity not managed; nested exception is java.lang.IllegalArgumentException: Entity not managed

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:492)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)

这条线在hibernate-core-5.2.1.final SessionImpl类返回 null

EntityEntry entry = persistenceContext.getEntry( object );

我认为它应该返回实体。

My persistence.xml file is

<persistence-unit name="testPersitanceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.sample.dao.gen.StudentEntity</class>
    <properties>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hbm2ddl.auto" value="update"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.enable_lazy_load_no_trans" value="true" />
    </properties>
</persistence-unit>

我的 spring db 配置文件是

 <jpa:repositories base-package="com.sample.dao.repos"
                  entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>

<!-- Enable the component scan (auto wiring etc) for the following package -->
<context:component-scan base-package="com.sample.dao" />

<!-- Make sure the following is specified to enable transaction  -->
<tx:annotation-driven />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!--  This defines the entity manager factory with some custom properties -->
<bean id='entityManagerFactory' primary="true" class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
    <property name="persistenceUnitName" value="testPersitanceUnit"/>
    <property name='dataSource' ref='dataSource' />
    <property name="packagesToScan" value="com.sample.dao" />
enter code here

try

public StudentEntity refresh(StudentEntity studentEntity) {
    StudentEntity managedEntity = entityManager.find(StudentEntity.class, studentEntity.getId());
    entityManager.refresh(managedEntity);
    return managedEntity;
}}

您遇到问题的原因是,在您的测试中,您的 StudentEntity 在事务 (findOne) 之外传递,因此不再受管理。当它被传递到新事务(刷新)时,它不受管理,因此会抛出此错误。

或者,如果原始刷新是所需的行为,您可以将整个测试包装在一个事务中,这应该维护对测试对象的管理。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring data Jpa实体不受管理调用刷新时出现异常 的相关文章

随机推荐

  • Textjoin:使用唯一 ID,如何连接多个值?

    我很难让它发挥作用 基本上 我有以下一组数据 在 TextJoin Formula 列中 我希望它查找相邻 ID 扫描收入代码列并连接与该唯一 ID 相关的收入代码 并用 分隔 非常感谢 use TEXTJOIN TRUE IF A 2 A
  • 即使 makefile 和源代码存在,为什么“Build Project”在 Eclipse Helios CDT 中显示为灰色?

    我无法构建我的项目 我在 Eclipse Helios 中创建了一个新的 CDT 项目 并告诉它使用现有的源代码和 makefile 这两者都正确显示在 Package 和 Project 视图中 然而 项目 菜单中的 构建全部 和 构建项
  • 使用 ps2pdf (ghostscript) 创建正确的 PDF/X

    我已经为此苦苦挣扎了几天 所以我想我应该在这里寻求帮助 基本上 我尝试使用 ps2pdf 版本 9 10 创建正确的 PDF X 1 和 PDF X 3 文档 是的 我知道据说 ps2pdf 仅支持 PDF X 3 请参阅这个线程 http
  • 如何匹配元音?

    我在处理我正在开发的一个更大程序的一个小组件时遇到了麻烦 基本上我需要让用户输入一个单词 并且需要打印第一个元音的索引 word raw input Enter word vowel aeiouAEIOU for index in word
  • 如何在自定义按钮单击时通过@selector传递数据?

    我正在通过代码制作一个按钮 我有以下代码行来在单击按钮时触发方法 imagesButton addTarget self action selector photoClicked forControlEvents UIControlEven
  • 您可以使用 Jekyll 的 _includes 文件夹中的子目录吗?

    我的计划是在 includes目录 包括 页脚 包括 英雄 includes cta etc 当我引用那个时 include footers footer1 html 我收到以下错误 Liquid Exception Included fi
  • 如何在 ASP.NET Core 中转换 AppSettings

    我被引导相信appsettings json及其环境对应项 开发 登台 生产 取代了 Web config 转换 这在开发环境中工作得很好 这些环境的变量存在于launchSettings json 但是 如果我使用发布功能 将发布配置为使
  • Adwhirl 的问题(Admob+Inmobi+..)

    使用 AdWhirl 我遇到了这些例外 我无法从我这边发现错误 任何人都可以建议这个 FATAL EXCEPTION main E AndroidRuntime 279 java lang NullPointerException E An
  • Google Apps 脚本:从云端硬盘下载文件(同一用户)

    我正在尝试编写一个 Google Apps 脚本来下载特定云端硬盘文件夹中的所有文件 可能是 csv 文件 我找到了 getDownloadUrl 方法 但我不知道该做什么do用它 我目前正在尝试以下代码 其中files是文件夹中的文件列表
  • IEnumerable.GetEnumerator() 和 IEnumerable.GetEnumerator()

    在 net框架中 有一个通用的IEnumerable
  • 在 Android 中使用 Facebook Achievement API

    我知道这可能看起来像一个通用问题 但找到有关该主题的信息似乎非常困难 因此 如果某个地方存在完整的示例 指南 源代码链接 我将不胜感激 我正在开发一款 Android 游戏 希望集成 Facebook 成就 我想要的只是在用户完成某个谜题时
  • Rails 4.0 expire_fragment/缓存过期不起作用

    我一直在尝试使用 Rails 的缓存功能 但我无法使某些缓存片段过期 尽管它们似乎已过期 使用 Rails 教程网站中指出的 Russian Doll Caching 我正在使用此配置 我使release controller rb 控制器
  • #DELETE 在 Access 中查看 SQL Server 表

    今天早上又出现了一个新问题 我的数据库驻留在 SQL Server 上 并使用 Access 作为前端 其中一个已经使用了至少 10 年的数据库今天突然停止工作 我发现这个问题影响了 2 个 可能更多 我没有检查所有 表 当我在访问中打开表
  • 如何使用 R / igraph 设置边缘颜色和顶点间距

    我是 R 新手 试图弄清楚如何为我有数据的系统制作社交网络地图 我已经设法从常见问题解答和教程中弄清楚我想做的大部分事情 但我被困在两件事上 如何使画布更大 图表间隔更大 现在已经太挤了 目前 边缘厚度是根据重量设置的 权重代表不同的状态
  • 如何查看哪个用户启动了 Docker 容器?

    我可以查看正在运行的容器的列表docker ps https docs docker com engine reference commandline ps or 同等地 https stackoverflow com q 45254677
  • J2me -Polish ---与创建和构建项目相关的问题

    我是 j2me polish 的初学者 我已经安装了 j2me polish2 1 4 按照下面链接中显示的步骤 http www j2mepolish org cms leftsection documentation installat
  • 如何抑制 C# 中方法调用的错误?

    我正在寻找一种 优雅 的方法来在调用方法时抑制异常 我认为下面的代码太冗长了 try CallToMethodThatMayFail 3 catch 是否有一些语法糖我可以用来说 我真的不在乎这个方法是否失败 我想调用该方法并继续执行 无论
  • 如何将正文中的数字替换为波斯数字?

    我想将 html 内容中的每个数字转换为波斯数字 而不会对页面元素产生其他影响 例如 div style color c2c2c2 text number 1 span text number 2 span div text number
  • 对“yylex()”的未定义引用

    我正在尝试使用 flex 和 bison 创建一种简单的脚本语言 现在 我只是想让计算器工作 但我无法编译它 当我运行这个 makefile 时 OBJECTS hug tab o hug yy o PROGRAM hug exe CPP
  • Spring data Jpa实体不受管理调用刷新时出现异常

    我有一个数据库代码 jar 我在不同的应用程序中使用它来访问数据库 我在用spring data jpa 我需要调用刷新来检查来自其他应用程序的数据库行的更改 这里我如何实现它 我的StudentRepository界面 public in