I have two entities BatchFcm and FcmTopics. One FcmTopic can belong to many BatchFcm, so it has a @ManyToOne relation. I try to save the batchFcm but its not getting saved.
I have already tried putting
@ManyToOne(cascade = CascadeType.PERSIST) FcmTopics fcmTopics I even tried CascadeType.ALL. But that is not my requirement. The fcmtopics table will have data already inserted in it, I need to pick one and set in the batchfcm database.
I tried the @Transient annotation, it worked but the fcmTopics in column always remains null. I want the column with the id of fcmtopics in it. Th Transient does stop mapping the field fcmtopics into a json data when retrieved from a controller. But I do want to retrieve it.
Here are my entities
BatchFcm
@Entity @Table(name = "batch_fcm") @Getter @Setter @NoArgsConstructor public class BatchFcm extends AbstractEntity { @ManyToOne private FCMTopics fcmTopics; FcmTopics
@Entity @Table(name = "fcm_topics") @Getter @Setter public class FCMTopics extends AbstractEntity { @Column(unique = true) private String name; private String description; private FCMTopicStatus status; When i use CascadeType i get a constraintViolation exception with the duplicate key, I know it is due to the unique = true . Below is my Service
BatchFcm fcmDetail; if (adminFcmResource.getId() != null) { log.info("Updating bulk fcm log for batch id: {}, admin: {}", adminFcmResource.getId(), adminUser); fcmDetail = batchFcmRepository.findOne(adminFcmResource.getId()); fcmDetail.setStatus(adminFcmResource.getStatus()); fcmDetail.setCreatedBy(adminFcmResource.getAdminId()); fcmDetail.setImagePath(adminFcmResource.getImagePath()); fcmDetail.setLogo(adminFcmResource.getLogo()); if (!InputUtil.isEmpty(adminFcmResource.getTopic())) { fcmDetail.setFcmTopics(fcmTopicsService.findByName(adminFcmResource.getTopic())); } else { fcmDetail.setCsvPath(adminFcmResource.getCsvPath()); } } else { log.info("Adding bulk fcm log with csv path: {}, admin: {}", adminFcmResource.getCsvPath(), adminUser); fcmDetail = new BatchFcm(adminFcmResource.getTitle(), adminFcmResource.getMessage(), adminFcmResource.getImagePath(), adminFcmResource.getAdminId(), adminFcmResource.getExternalRedirectUrl()); if (!InputUtil.isEmpty(adminFcmResource.getTopic())) { fcmDetail.setFcmTopics(fcmTopicsService.findByName(adminFcmResource.getTopic())); } else { fcmDetail.setCsvPath(adminFcmResource.getCsvPath()); } fcmDetail.setLogo(adminFcmResource.getLogo()); fcmDetail.setForScheduling(false); } fcmDetail.setFcmType(FCMTypes.OFFERS.ordinal()); fcmDetail.setCreatedBy(AuthUtil.getCurrentUser().getId()); batchFcmRepository.save(fcmDetail); The findByName does have a @Cacheable though. Is there a way to save the data? and also keep the id in the column. I have done @JoinColumn(referencedColumnName = "id", name = "fcm_topics_id").