4

是否可以轻松定义对外部字段的约束?

示例:我有一个 Entity1 和一个 Entity2。Entity1 有一个字段 entity2_id,它是建立连接的外键。然而,这并不像 @Required 对简单字段的约束一样有效,例如String title

这不起作用:

@ManyToOne
@JoinColumn(name = "entity2_id")
@Required
public Entity2 entity2;

这有效:

@Required
public String title;

我知道表单表单validate()方法。有没有更简单的方法?最好是一种在视图中的特定字段上强制错误而不将消息作为参数传递给视图的方法。

谢谢

4

1 回答 1

1

I have use Required annotation on few ManyToOne relationship, this work perfectly.

One diference with your code is the JoinColumn annotation that i don't use.

I'm aware about the form form validate() method. Is there a simpler way? preferrably a way that will force the error on the specific field in the view without passing the message to the view as a parameter.

Only solution i know is to create your own annotation.

于 2015-02-03T09:37:42.747 回答