gpt4 book ai didi

java - 使用存储库的异步方法中的 Spring InvalidDataAccessApiUsageException

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:58 26 4
gpt4 key购买 nike

在 RestController 中:

@RestController
@RequestMapping("/apks")
public class ApkController {
@Inject
DecompiledApkRepository decompiledApkRepository;

@Autowired
DecompileService decompileService;

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> createFromJson(@RequestBody Apk apk) {
....
DecompiledApk decompiledApk = new DecompiledApk(apk, apkFile, apk.getMd5Hash());
decompileService.decompile(decompiledApk, apk.getMd5Hash(), decompiledApkRepository);

} catch (IOException |InterruptedException e) {
e.printStackTrace();
}

return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}

DecompiledApk 实体:

@Entity
@Table(name = "decompiled_apks")
public class DecompiledApk {

@Id
@GeneratedValue
@JsonIgnore
private Long id;

@MapsId
@OneToOne(optional=false)
private Apk apk;

@Column(unique = true)
private URI decompiledFolder;

@Transient
@JsonIgnore
private File inputApk;

// all public getters/setters, package empty constructor and public full constructor

反编译的Apk存储库:

@Repository
public interface DecompiledApkRepository extends CrudRepository<DecompiledApk, Long> {
DecompiledApk findByApk_md5Hash(String md5Hash);
}

这里,问题出在 DecompileService 中的异步方法中:

@Service
public class DecompileService {
private static final String DEC_FOLDER = "/tmp/decompiled/";


@Async
public Future<Void> decompile(DecompiledApk decompiledApk, String md5Hash, DecompiledApkRepository decompiledApkRepository) throws InterruptedException {
/*
...
*/
decompiledApk.setDecompiledFolder(URI.create(outputFolder));
System.err.println("---start-->"+Thread.currentThread().getName());
decompiledApkRepository.save(decompiledApk);
System.err.println("---end-->"+Thread.currentThread().getName());
return new AsyncResult<>(null);
}

说明:

decompiledApkRepository.save(decompiledApk);

抛出:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.xxx.domain.Apk; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.xxx.domain.Apk

但是如果我删除 @Async 注释,它就可以正常工作!有任何想法吗?您需要更多详细信息吗?

最佳答案

虽然该方法已被标记为异步,但没有 spring 事务标记,怀疑是导致问题的原因。尝试添加 Spring 事务:

org.springframework.transaction.annotation  


@Transactional

希望这能解决问题。

关于java - 使用存储库的异步方法中的 Spring InvalidDataAccessApiUsageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35316535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com