I have a model :
public class ABC implements Serializable {
private int baseId;
private Integer aId;
private Integer bId;
private Boolean isReal;
private TimeStamp updateTime;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "base_id", nullable = false)
public int getBaseId() {
return baseId;
}
public void setBaseId(int baseId) {
this.baseId = baseId;
}
@Basic
@Column(name = "a_id", nullable = false)
public Integer getAId() {
return aId;
}
public void setAId(Integer aId) {
this.aId = aId;
}
@Basic
@Column(name = "b_id", nullable = false)
public Integer getBId() {
return bId;
}
public void setBId(Integer bId) {
this.bId = bId;
}
@Basic
@Column(name = "is_real")
public Boolean getIsReal() {
return isReal;
}
public void setIsReal(Boolean isReal) {
this.isReal = isReal;
}
@Basic
@Column(name ="update_time")
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
}
I have a controller Class:
@RestController
@RequestMapping(path = "${serverconfig.api-base-path}/base")
public class BaseController {
/**
* Instance of an logger
*/
private static final Logger LOG =
LoggerFactory.getLogger(BaseController.class);
/**
* Base repository
*/
private BaseRepository baseRepository;
/***
*
* @param baseRepository
*/
public BaseController(BaseRepository baseRepository) {
LOG.trace("BaseRepository constructor method.");
this.baseRepository = baseRepository;
}
@PostMapping(path = Route.UPDATE_IS_REAL)
// @Transactional
public ABC updateIsReal(@Valid @RequestBody
@RequestParam("baseId") int baseId,
@RequestParam("isReal") boolean isReal){
ABC abc = baseRepository.findByBaseId(baseId);
Date date= new Date();
Timestamp ts = new Timestamp(date.getTime());
abc.setBaseId(baseId);
abc.setIsReal(isReal);
abc.setUpdateTime(ts);
return baseRepository.save(abc);
}
}
My repository class:
@Repository
public interface BaseRepository extends
JpaRepository<ABC, Integer> {
List<ABC> findByAId(Integer aId);
ABC findByBaseId(Integer baseId);
}
Database table has an entry :
"base_id": 1,
"a_Id": 1,
"b_Id": 1,
"is_real": null,
"update_time": null
When I call the endpoint it gives no error and returns:
"base_id": 1,
"aId": 1,
"bId": 1,
"isReal": yes,
"updateTime": 018-10-01T18:30:56.765+0000
But When I query the database, the record is not updated there. I am not understanding what I am doing wrong. I am supplying id when I try to make a rest call and that id exists in the database.
With save
, changes won't necessary be flushed to DB immediately and might stay just in memory, until flush or commit commands are issued.
With saveAndFlush
, changes will be flushed to DB immediately.
However, if you flush the changes in transaction and do not commit them, the changes still won't be visible to the outside transactions until the commit in this transaction.
In your BaseController try changing
return baseRepository.save(abc);
to
return baseRepository.saveAndFlush(abc);
Further information here and here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With