小编zac*_*ach的帖子

如何访问注释属性中描述的字段

是否可以访问字段值,其中字段名称在注释中描述,注释对类中的另一个字段进行注释。

例如:

@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)

java reflection annotations hibernate-validator

7
推荐指数
2
解决办法
7851
查看次数

已尝试附加或添加非新的实体,可能已从另一个DataContext加载

我遇到了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)

c# linq linq-to-sql notsupportedexception

5
推荐指数
1
解决办法
9353
查看次数

C#如何将从linq返回的对象分配给'this'引用?

好的,这是我正在尝试解决的问题 - 我想通过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)

c#

3
推荐指数
1
解决办法
412
查看次数