我有以下代码:
$sql = "Select * from table_name where catregory = ".$_POST'category'];
// execute the query.
$sub = "<select name='sub-category'>";
foreach(result from database as $row) {
$sub .= "<option value='$row->id'>$row->name</option>";
}
$sub .= "</select>";
echo $sub;
其中UniqueConstraintViolation是接口扩展的javax.validation.ConstraintViolation 和UniqueConstraintViolationImpl是类实现的UniqueConstraintViolation
编译时我得到以下错误
java:42:错误:找不到合适的方法 添加(UniqueConstraintViolation) violations.add(冲突); ^ 方法Collection.add(CAP#1)不适用 (参数不匹配; UniqueConstraintViolation无法转换为CAP#1) 方法Set.add(CAP#1)不适用 (参数不匹配; UniqueConstraintViolation无法转换为CAP#1),其中CAP#1是一个新的类型变量: CAP#1扩展了ConstraintViolation的捕获?扩展ConstraintViolation C:\ workspace \ zcts-travel \ crud-rest \ src \ main \ java \ ru \ zcts \ crud \ AbstractResource.java:43:错误:不兼容类型:设置无法转换为设置&gt; 抛出新的javax.validation.ConstraintViolationException(违规);
UniqueConstraintViolation violation = new UniqueConstraintViolationImpl(); Set<? extends javax.validation.ConstraintViolation> violations = new HashSet<UniqueConstraintViolation>(); violations.add(violation); throw new javax.validation.ConstraintViolationException(violations);
我只想传递javax.validation.ConstraintViolationException构造函数,使用我自己的javax.validation.ConstraintViolation实现。 我怎么能这样做?
答案 0 :(得分:1)
Set<? extends javax.validation.ConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
您无法将null
以外的任何内容添加到voilations
,因为它使用? extends javax.validation.ConstraintViolation
。
请在此处阅读:Java Generic with ArrayList <? extends A> add element
答案 1 :(得分:1)
所以有你的约束:
interface UniqueConstraintViolation extends ConstraintViolation<YourClass> {
}
public class UniqueConstraintViolationImpl implements UniqueConstraintViolation {
/* your impl here */
}
这将编译:
UniqueConstraintViolation violation = new UniqueConstraintViolationImpl();
Set<UniqueConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
throw new ConstraintViolationException(violations);