I have two entities First
and Second
. There are @PreRemove
and PostRemove
implementations for First
. The preRemove()
creates a record in Second
and caches the result to a transient property First.second
. This is working. I want the postRemove
to update the same record, which is not working. It is not updating the record.
@Entity
@Getter@Setter@NoArgsConstructor@AllArgsConstructor
public class Second {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
private String prop;
}
@Entity
@Getter@Setter@NoArgsConstructor@AllArgsConstructor
public class First {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String prop;
@Transient@JsonIgnore
private Second second;
@PreRemove
public void preRemove() {
second = MyBeanUtils.getBean(SecondRepository.class).save(new Second());
}
@PostRemove
public void postRemove() {
second.setProp("after remove");
MyBeanUtils.getBean(SecondRepository.class).save(second);
}
}
Controller and Repository codes
@Repository
public interface FirstRepository extends JpaRepository<First, Long> {
}
@Repository
public interface SecondRepository extends JpaRepository<Second, Long> {
}
@RestController
@RequiredArgsConstructor
public class DemoController {
private final FirstRepository repository;
@PostMapping("/first")
public First postBo3(@RequestBody First bo3) {
return repository.save(bo3);
}
@DeleteMapping("/first/{id}")
void postBo3(@PathVariable long id) {
repository.deleteById(id);
}
}
When I put a break point on the line second.setProp("after remove");
in the postRemove()
, it shows the field second
does have an ID, because it is the cached instance after save()
.
With spring.jpa.show-sql=true
, these are the queries logged. Can't see any update query and the record is not updated in the database too. Please suggest a reason for this and a workaround.
Hibernate: select first0_.id as id1_0_0_, first0_.prop as prop2_0_0_ from first first0_ where first0_.id=?
Hibernate: insert into second (prop) values (?)
Hibernate: delete from first where id=?