是否可以访问字段值,其中字段名称在注释中描述,注释对类中的另一个字段进行注释。
例如:
@Entity
public class User {
@NotBlank
private String password;
@Match(field = "password")
private String passwordConfirmation;
}
Run Code Online (Sandbox Code Playgroud)
注解:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface Match {
String message() default "{}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field();
}
Run Code Online (Sandbox Code Playgroud)
现在,是否可以从 ConstraintValidator 实现类中的 User 类访问字段密码?
编辑:
我写了这样的东西:
public class MatchValidator implements ConstraintValidator<Match, Object> {
private String mainField;
private String secondField;
private Class clazz;
@Override
public void initialize(final Match match) {
clazz = User.class;
final …Run Code Online (Sandbox Code Playgroud) 我遇到了NotSupportedException的问题,我得到:"尝试附加或添加一个非新的实体,可能是从另一个DataContext加载的."
partial class SupplyOfert : Model
{
public SupplyOfert(int id = 0)
{
if (id > 0)
this.get(id);
}
public Supplier supplier
{
get
{
return this.Supplier;
}
set
{
this.Supplier = db.Suppliers.SingleOrDefault(s => s.id == value.id);
}
}
public override bool get(int id)
{
SupplyOfert so = (SupplyOfert)db.SupplyOferts.SingleOrDefault(s => s.id == id).Copy(this, "Supplies");
this._Supplies = so._Supplies;
so._Supplies.Clear();
if (this.id > 0)
{
db.Dispose();
db = new Database();
db.SupplyOferts.Attach(this); // here I'm getting the exception
return true;
}
else
return …Run Code Online (Sandbox Code Playgroud) 好的,这是我正在尝试解决的问题 - 我想通过linq从db返回返回的对象特性到此对象属性,并且可以更新任何更改,而无需在db中插入新行.
partial class Employee : Person
{
public Employee(int id = 0)
{
if (id > 0)
this.get(id);
}
public override bool get(int id)
{
Employee empl = db.Employees.Single(Employee => Employee.id == id);
if (empl != null)
{
// here I need to do sth like
this = empl;
// or
ObjectProperties.Copy(empl, this);
return true;
}
else
return false;
}
public override void save()
{
if (this.id > 0)
{
db.SubmitChanges();
}
else
{
db.Employees.InsertOnSubmit(this);
db.SubmitChanges();
}
} …Run Code Online (Sandbox Code Playgroud)