这里是我的实体:
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@Entity
@Table(name = "GITTBUSUARIS")
public class Usuari {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USUARIS_ID")
private Integer id;
@Column(name = "USUARIS_CODI")
private String codi;
@Column(name = "USUARIS_NOM")
private String nom;
// @OneToMany(cascade = CascadeType.ALL, mappedBy = "id.usuari", fetch = FetchType.EAGER)
// private Set<PermisUsuari> permisos;
}
如您所见,permisos
字段已被注释。
这里是我的存储库:
@RequiredArgsConstructor
public class ReactiveJpaSpecificationExecutorBase<T> implements ReactiveJpaSpecificationExecutor<T> {
private final Mutiny.SessionFactory sessionFactory;
@Override
public Mono<T> findOne(Specification<T> spec) {
CriteriaQuery<T> criteriaQuery = this.buildQuery(spec, Sort.unsorted());
return this.sessionFactory
.withSession(session -> session.createQuery(criteriaQuery).getSingleResultOrNull())
.convert().with(UniReactorConverters.toMono());
}
}
上面的代码工作正常。然而,当我取消注释permisos
字段时,我会得到以下异常消息:
Failed to execute statement [select rol0_.ROLS_ID as rols_id1_12_0_, rol0_.ROLS_CODI as rols_cod2_12_0_, rol0_.ROLS_DENOMINACIO as rols_den3_12_0_ from GITTBROLS rol0_ where rol0_.ROLS_ID=@P1]: could not load an entity: [cat.gencat.clt.git.backend.model.persistency.Rol#1]
java.util.concurrent.CompletionException: java.lang.IllegalStateException: Session/EntityManager is closed
我正在到达我的代码:
@RestController
@RequestMapping(value = "/qdcf")
@RequiredArgsConstructor
@Timed
public class QdCFController {
private final QdCFService qdcfService;
private final UsuariRepository usuariRepository;
@GetMapping("/user")
public Mono<Usuari> user() {
return this.usuariRepository.findOne(UsuariSpecs.hasCodi("11111111A"));
}
}
如何保持会话按线程打开?
发生此错误是因为应用程序正在尝试加载实体Rol
。您在问题中没有显示与Rol
的关联,但我认为这是一个懒惰的关联。
Hibernate Reactive不像Hibernate ORM那样按需加载惰性关联。如果是这样的话,你有两个选择:
- 在条件中使用fetch联接急切地加载关联
- 获得结果后获取角色。假设关联被称为
roles
,它应该看起来像:return this.sessionFactory .withSession(session -> session .createQuery(criteriaQuery).getSingleResultOrNull() .onItem().ifNotNull().call( result -> Mutiny.fetch(result.getRoles()) ) ) .convert().with(UniReactorConverters.toMono());
我需要查看所有相关实体的映射,以帮助您获得更多