0

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").

3 Answers 3

1

Please set the version in database table as hibernate tries to update the transient property on the basis of version.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, my version was NULL , then i changed it 0, then it worked.
0

One problem that i see here is that you are missing the @id annotation in both entities. It can happen, that even your topic already exists in your DB, hibernate doesnt see the id, and thinks it is a new entry, that has to be inserted. Try to add the id fields in your entities.

2 Comments

The id annotation is present in the AbstractEntity
Are you using @Transactional annotation on your method?
0

Means that you forget to save state of the object

1 Comment

I tried using CascadeType.PERSIST or CascadeType.ALL but due to the unique =true a ConstraintViolationException occurs. It does save the instance, doesnt it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.