7

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.

4
  • Is your entity mapped with the @Entity annotation ? Oct 1, 2018 at 19:39
  • yes, I have Entity annotation Oct 2, 2018 at 0:52
  • There is no such "JPA save method". That is Spring Data JPA, a totally different API. FYI
    – user3973283
    Oct 2, 2018 at 6:15
  • I had this error because of putting @Transactional on a Junit test class, which automatically rolls back db transactions. Apr 2, 2020 at 16:47

2 Answers 2

8

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

3
  • 4
    When I use saveAndFlush I get : no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progres Oct 2, 2018 at 0:59
  • 5
    @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
    – Dazak
    Oct 3, 2018 at 4:40
  • 1
    Thank 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. Oct 9, 2018 at 13:27
4

yes, it works just replace "create" with "update"

spring.jpa.hibernate.ddl-auto=update

1
  • 1
    I upvoted @SydMK 's answer because it was inspiring. In my case, it was something very similar. I initially had this prop set to spring.jpa.hibernate.ddl-auto=update and I had Liquibase changeSets which added complexity to the DB landscape, so sometimes my save() call to JPA repository could save sometimes it couldn't and errored out with all kinds of nasty messages. Changing this prop to =create-drop gave me a cleaner slate and helped greatly to avoid this symptom. Aug 23, 2022 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.