belgoros
belgoros

Reputation: 3908

Using with JSF - HIbernate validation fails (javax.validation.ConstraintViolationException)

I'm using JSF, Spring and Hibernate. Post model has Hibernate annotated attributes:

@Entity
@Table(name = "posts")
public class Post implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    private Long id;

    private int version;

    @Column(name = "title")
    @NotEmpty(message = "Title should not be empty")
    private String title;

    ... getters/setters

}

When I try to create a new Post in the corresponding bean(disregard syntax proper à pretty-faces) :

@Component
@ManagedBean
@RequestScoped
@URLMappings(mappings = {
        @URLMapping(id = "posts", pattern = "/posts/", viewId = "/faces/posts/list.xhtml"),
        @URLMapping(id = "new", pattern = "/posts/new", viewId = "/faces/posts/new.xhtml")
})
public class PostBean {

    @Autowired
    private PostService postService;

    private List<Post> posts;

    private Post post = new Post();

    public List<Post> getPosts() {
        return postService.findAll();
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public String create(Post post) {
        this.post = postService.save(post);
        return "pretty:posts";
    }
}

PostRepository:

public interface PostRepository extends CrudRepository<Post, Long> {
    public Post findByTitleIgnoreCase(String title);
}

PostServiceImpl:

@Service("postService")
@Repository
@Transactional
public class PostServiceImpl implements PostService {

    @Autowired
    private PostRepository postRepository;

    @Override
    @Transactional(readOnly = true)
    public List<Post> findAll() {
        return Lists.newArrayList(postRepository.findAll());
    }

    @Override
    @Transactional(readOnly = true)
    public Post findById(Long id) {
        return postRepository.findOne(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Post findByTitleIgnoreCase(String title) {
        return postRepository.findByTitleIgnoreCase(title);
    }


    @Override
    @Transactional
    public Post save(Post post) {
        return postRepository.save(post);
    }

    @Override
    public void destroy(Post post) {
        postRepository.delete(post);
    }
}

I get the javax.validation.ConstraintViolationException. Strange is that if remove Hibernate annotation and put required="true" directly into the JSF page aside to the title textfield, it works.

<h:inputText id="title" value="#{postBean.post.title}" required="true"/>

Any idea ? You can find the project code source at my github repo Thank you.

@NotNull is a JSR303 validation, @NotEmpty - is the one by Hibernate, that is the only difference. Even after changing for @NotNull I still get the same error.

[update] By the way, I removed @ManagedBean annotation, it is not needed anymore. [update-2] @AVolpe: It will change nothing, just move the message to be displayed aside of the title textfield. The error happens when calling:

public String create(Post post) {
        this.post = postService.save(post);
        return "pretty:posts";
    }

Upvotes: 0

Views: 375

Answers (2)

Arturo Volpe
Arturo Volpe

Reputation: 3627

Because this error the version 2.2.6 of JSF don't work well with BeanValidation, update to the version 2.2.7 or downgrade to 2.2.4.

See: This answer

Upvotes: 1

belgoros
belgoros

Reputation: 3908

Fixed by upgrading jsf version up 2.2.7 as suggested by AVolpe.

Upvotes: 0

Related Questions