Reputation: 901
I'm executing a method at startup time with @PostConstruct
annotation. This method has to check a value stored in a table in DB. If it doesn't exist, then it has to insert it. The checking of the value in DB is done correctly, however if I had to persist it, it won't write it to DB. It doesn't throw any exception and persist()
is executed (apparently) without problems, only the value is not inserted in DB.
Moreover, once everything is up, if I manually call that method (from a Controller, for example), it will insert the value correctly.
@PostConstruct
public void insertIfNecessary()
{
Request r = Request.findRequestForUser(this.me);
if ( r == null )
{
r = new Request();
r.setOwner(this.me);
r.persist();
}
}
Do you know what may be wrong?
Best regards, Miguel
Upvotes: 1
Views: 641
Reputation: 120811
Make sure that the transaction is commited, not rolled back.
add @Transactional to the method
Added:
The second thing should make sure is that the @Transactional(readonly=false)
annotation is taken in account! If you use Spring Proxy AOP (not AspectJ) then this means, that the method annotated with @Transactional(readonly=false)
must bean (directly) invoked from an other bean.
Upvotes: 1