我遇到了jhipster和spring数据jpa的问题。 我有一个像这样的经典JHipster JPA实体:
@Entity
@Table(name = "PROCEEDINGSINSTANCE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName="proceedingsinstance")
public class ProceedingsInstance implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "proceedings_bpm_code")
private Integer proceedingsBpmCode;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonSerialize(using = CustomDateTimeSerializer.class)
@JsonDeserialize(using = CustomDateTimeDeserializer.class)
@Column(name = "start_date")
private DateTime startDate;
@Column(name = "description")
private String description;
@ManyToOne
private AgencyProceedings agencyProceedings;
@ManyToOne
private User user;
@ManyToOne
private Outcome outcome;
@ManyToOne
private Status status;
@OneToMany(mappedBy = "proceedingsInstance", fetch = FetchType.LAZY)
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Activity> activitys = new HashSet<>();
@OneToMany(mappedBy = "proceedingsInstance", fetch = FetchType.LAZY)
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<MyDocument> myDocuments = new HashSet<>();
... getters/setters...
}
这样的服务类:
@Service
@Transactional
public class ProceedingsInstanceService {
...bean injection...
public ProceedingsInstance createByAgencyProceedings(
Long agencyProceedingsId) {
AgencyProceedings agencyProceedings = agencyProceedingsRepository.findOne(agencyProceedingsId);
if (agencyProceedings == null) {
return null;
}
User user = userService.getUserWithAuthorities();
ProceedingsInstance existingProceedingsInstance = proceedingsInstanceRepository.findOneByAgencyProceedingsIdAndUserIdAndStatusIdNot(agencyProceedingsId,
user.getId(), STATUS_COMPLETED.getId());
if (existingProceedingsInstance != null) {
return existingProceedingsInstance;
}
agencyProceedings.getAgencyModules().size();
ProceedingsInstance proceedingsInstance = proceedingsInstanceMapper.agencyProceedingsToProceedingsInstance(agencyProceedings);
proceedingsInstance.setOutcome(OUTCOME_WAITING);
proceedingsInstance.setStatus(STATUS_STARTED);
proceedingsInstance.setStartDate(new DateTime());
proceedingsInstance.setUser(user);
proceedingsInstanceRepository.save(proceedingsInstance);
proceedingsInstanceSearchRepository.save(proceedingsInstance);
myDocumentRepository.save(proceedingsInstance.getMyDocuments());
return proceedingsInstance;
}
public ProceedingsInstance saveFile(Long id) {
return changeProceedingsInstanceStatus(id, STATUS_SAVED);
}
public ProceedingsInstance sendFile(Long id) throws JsonProcessingException, JMSException {
ProceedingsInstance proceedingsInstance = changeProceedingsInstanceStatus(id, STATUS_SENDED);
if (proceedingsInstance == null) {
return null;
}
JmsPortalQueue jmsPortalQueue = jmsPortalQueueMapper.proceedingsInstanceToJmsPortalQueueIntegrazione(proceedingsInstance,
userService.getUserWithAuthorities());
jmsService.sendToPortalQueue(jmsPortalQueue);
return proceedingsInstance;
}
private ProceedingsInstance changeProceedingsInstanceStatus(Long id, final Status status) {
ProceedingsInstance proceedingsInstance = proceedingsInstanceRepository.findOne(id);
if (proceedingsInstance == null) {
return null;
}
proceedingsInstance.getActivitys().size();
Set<Activity> activities = proceedingsInstance.getActivitys();
Optional<Activity> activityOpt = activities.parallelStream()
.filter(activity -> status.getCode().equals(activity.getStatus().getCode()) && activity.getEndDate() == null).findFirst();
if (activityOpt.isPresent()) {
return proceedingsInstance;
}
Optional<Activity> activityToCloseOpt = activities.parallelStream()
.filter(activity -> !status.getCode().equals(activity.getStatus().getCode()) && activity.getEndDate() == null).findFirst();
if (activityToCloseOpt.isPresent()) {
Activity activityToClose = activityToCloseOpt.get();
activityToClose.setEndDate(new DateTime());
activityRepository.save(activityToClose);
}
Activity activity = new Activity();
activity.setStatus(status);
activity.setStartDate(new DateTime());
activity.setProceedingsInstance(proceedingsInstance);
activityRepository.save(activity);
proceedingsInstance.setStatus(status);
proceedingsInstanceRepository.save(proceedingsInstance);
proceedingsInstanceSearchRepository.save(proceedingsInstance);
return proceedingsInstance;
}
@PostConstruct
public void init() {
OUTCOME_WAITING = outcomeRepository.findByCode(OUTCOME_CODE_WAITING);
STATUS_STARTED = statusRepository.findByCode(STATUS_CODE_STARTED);
STATUS_SAVED = statusRepository.findByCode(STATUS_CODE_SAVED);
STATUS_SENDED = statusRepository.findByCode(STATUS_CODE_SENDED);
STATUS_COMPLETED = statusRepository.findByCode(STATUS_CODE_COMPLETED);
}
}
每个方法都通过rest调用方法调用。执行流程是:
问题是:如果我两次调用saveFile, proceedingsInstance.getActivitys()。size()即使在相关表(ACTIVITY)中,第二次也是ZERO数据与正确的外键一起出现 我哪里错了?
编辑1: 我认为问题与缓存有关,因为在以这种方式更改@Cache注释之后:
@Cache(usage = CacheConcurrencyStrategy.NONE)
方法 saveFile 正常工作。 有人可以解释我如何更新缓存?
答案 0 :(得分:0)
您应该尝试在您的关系中设置级联。
@OneToMany(cascade={CascadeType.ALL})