I have a @PostConstruct, but it throws TransactionRequiredException using JPA.
How could I fix it?
@PostConstruct is called after the bean has been created but before any methods on the bean have been called. Within the @PostConstruct callback you can not be guaranteed that the transaction will have been initialized. You may be able to create a User Transaction or call this bean from another bean but in general the timing of the transaction vs the injection/creation will be Server dependent.
A lazy init of the bean will probably be a better choice in this case.
Thanks for your answer!
What does "lazy init" mean, is it a bean function called separately by the client after bean creation (public void init())? I' doing that now, but I thought it would be more "beautiful" doing initializer tasks in @PostContruct.
So a user transaction may work? Can I use it in @PostConstruct while I use container-managed transaction in other bean members? (is it possible to use both in an EJB?)
Yes what you are doing is what I meant by 'lazy init'. The user transaction would have to be active before you access the bean but you really couldn't mix container managed with bean managed in the same call stack as in most cases the bean managed (user transaction) would take precedence and the container would not manage the transaction.
Your use of this web site or any of its content or software indicates your agreement to be bound by these Terms of Participation.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.