i have a commandButton
, which calls a method inside the backing bean. This bean is ViewScoped and calls a service method, which is ApplicationScoped(inside there it calls repository method which saves the data from the form inside a database).
Now the service and repository methods run normal, but the code after those calls isnt run(e.g. the log and the redirect).
Here is the backing bean:
@Named
@ViewScoped
public class SubmitController implements Serializable {
private static final Logger LOG = LoggerFactory.getLogger(SubmitController.class);
private String userName;
private String title;
private String url;
private String text;
@Inject
PostService postService;
@PostConstruct
void init() {
LOG.info("Initialize SubmitController");
}
public void submitPost() throws IOException {
long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
LOG.info("postID: {}", postID);
FacesContext.getCurrentInstance()
.getExternalContext()
.redirect("item.xhtml");
}
}
The service:
@Service
public class PostService {
private static final Logger LOG = LoggerFactory.getLogger(PostService.class);
@Inject
PostRepository postRepository;
public long submitPost(final String userName, final String title, final String url, final String text) throws IOException {
Post post = Post.newPost()
.withUser(new User(userName))
.withTitle(new Title(title))
.withUrl(new Url(url))
.withText(new Text(text))
.build();
LOG.info("Submit post: {}", post.toString());
this.postRepository.submitPost(post);
Post refrence = this.postRepository.getRefrence(post);
LOG.info("Refrence: {}", refrence.toString());
return refrence.getId();
}
}
and the repository:
@Repository
public class PostRepository {
private static final Logger LOG = LoggerFactory.getLogger(PostRepository.class);
@PersistenceContext(unitName = "default")
EntityManager entityManager;
public Post getRefrence(final Post post) {
return this.entityManager.getReference(Post.class, post);
}
@Transactional
public void submitPost(final Post post) {
LOG.info("Save post");
this.entityManager.persist(post);
}
}
Note that the Repository and Service annotation are ApplicationScoped.
Has it something to do with the scopes or why isn´t the code after long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
run?
I didn't add the @Transactional
annotation to the repository method. That fixed my poblem