Spring Data JPA .save() method is not saving in the database
Solution 1
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
Solution 2
If someone was here for the same reason and didn't find an answer..:
Did you check if you have following set? Perhaps the whole schema may be getting re-created?
spring.jpa.hibernate.ddl-auto=create
nisha kanani
Updated on July 18, 2020Comments
-
nisha kanani almost 4 years
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.
-
nisha kanani over 5 yearsWhen I use saveAndFlush I get : no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progres
-
Dazak over 5 years@nishakanani I think you have to use transaction annotation, but first I think you have to refactor your code, it is not a good practice to go directly from controllers layer to repository layer without passing for a service layer
-
nisha kanani over 5 yearsThank you @Dazak. I agree. I added a service layer and Transaction annotation worked. I had two schemas and one schema was set as primary schema. I was trying to refer to another schema and it was not working as I was not using Transaction annotation.