我正在使用JSF,Spring和Hibernate。 Post模型具有Hibernate注释属性:
@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
}
当我尝试在相应的bean中创建一个新帖子时(忽略语法正确的à漂亮面孔):
@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);
}
}
我得到javax.validation.ConstraintViolationException
。奇怪的是,如果删除Hibernate注释并将required="true"
直接放到标题文本字段旁边的JSF页面中,它就可以工作。
<h:inputText id="title" value="#{postBean.post.title}" required="true"/>
有什么想法吗?您可以在my github repo找到项目代码来源 谢谢。
@NotNull是一个JSR303验证,@ NotEmpty - 是Hibernate的一个,这是唯一的区别。即使在更改了@NotNull后,我仍然会遇到同样的错误。
[update]顺便说一句,我删除了@ManagedBean注释,不再需要它了。 [update-2] @AVolpe:它不会改变任何内容,只需将消息移动到标题文本字段旁边即可。调用时发生错误:
public String create(Post post) {
this.post = postService.save(post);
return "pretty:posts";
}
答案 0 :(得分:1)
由于2.2.6
JSF
的版本BeanValidation
不能与2.2.7
配合使用,因此请更新至版本2.2.4
或降级至{{ 1}}。
请参阅: this error
答案 1 :(得分:0)
通过按照AVolpe的建议升级jsf版本2.2.7进行修复。