hibernate验证器不适用于bytebuddy生成的java类

k0pti3hp  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(312)

我的模型的域模型 PurchaseOrder

<?xml version="1.0" encoding="UTF-8"?>
<domain-models xmlns="http://axelor.com/xml/ns/domain-models"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://axelor.com/xml/ns/domain-models http://axelor.com/xml/ns/domain-models/domain-models_5.3.xsd">

  <module name="purchase" package="com.axelor.apps.purchase.db"/>

  <entity name="PurchaseOrder" lang="java">

    <string name="fullName" namecolumn="true">
    <![CDATA[
        if(purchaseOrderSeq==null){
            return " ";
        }

        if(supplierPartner == null) {
            return purchaseOrderSeq;
        }

        return purchaseOrderSeq + "-" + supplierPartner.getName();
    ]]>
    </string>
    <string name="purchaseOrderSeq" title="Ref." readonly="true"/>
    <many-to-one name="company" ref="com.axelor.apps.base.db.Company" required="true" title="Company"/>

    <many-to-one name="supplierPartner" ref="com.axelor.apps.base.db.Partner" required="true" title="Supplier"/>
    <many-to-one name="contactPartner" ref="com.axelor.apps.base.db.Partner" title="Contact"/>
    <many-to-one name="priceList" ref="com.axelor.apps.base.db.PriceList" title="Price list"/>

    <many-to-one name="team" ref="com.axelor.team.db.Team" title="Team" />
    <many-to-one name="buyerUser" ref="com.axelor.auth.db.User" title="Buyer"/>
    <many-to-one name="currency" ref="com.axelor.apps.base.db.Currency" title="Currency" required="true"/>
    <date name="deliveryDate" title="Estimated delivery Date"/>

....
....

生成的java文件如下所示(由bytebuddy生成)

public class PurchaseOrder extends AuditableModel {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PURCHASE_PURCHASE_ORDER_SEQ")
@SequenceGenerator(name = "PURCHASE_PURCHASE_ORDER_SEQ", sequenceName = "PURCHASE_PURCHASE_ORDER_SEQ", allocationSize = 1)
private Long id;

@NameColumn
@VirtualColumn
@Access(AccessType.PROPERTY)
private String fullName;

@Widget(title = "Ref.", readonly = true)
private String purchaseOrderSeq;

@Widget(title = "Company")
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Company company;

@Widget(title = "Supplier")
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Partner supplierPartner;

@Widget(title = "Currency")
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Currency currency;
....
....

如你所见 company, supplierPartner and currency 由注解 @NotNull 当我执行以下代码时,

public void validate(PurchaseOrder purchaseOrder) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    logger.info("======================= FETCHING VALIDATION ERRORS============================");

    logger.info("COMPANY = {}", purchaseOrder.getCompany());
    logger.info("SUPPLIER = {}", purchaseOrder.getSupplierPartner());
    logger.info("CURRENCY = {}", purchaseOrder.getCurrency());

    Set<ConstraintViolation<PurchaseOrder>> violations = validator.validate(purchaseOrder);
    for (ConstraintViolation<PurchaseOrder> violation : violations) {
        logger.error("{}", violation);
    }

}

生成的日志是

c.a.a.p.s.PurchaseOrderServiceImpl       : ======================= FETCHING VALIDATION ERRORS============================

c.a.a.p.s.PurchaseOrderServiceImpl       : COMPANY = Company{id=2, name=TEMP, code=052477, customerPaymentDelay=0.00, supplierPaymentDelay=0.00, defaultPartnerTypeSelect=1, width=0, height=0}

c.a.a.p.s.PurchaseOrderServiceImpl       : SUPPLIER = Partner{id=1, partnerSeq=P0001, partnerTypeSelect=1, titleSelect=0, name=SUPP, isCustomer=false, isProspect=false, isSupplier=true, isEmployee=false, isContact=false, fax=+910000000}

c.a.a.p.s.PurchaseOrderServiceImpl       : CURRENCY = Currency{id=64, name=Indian Rupee, code=INR, symbol=₹}

c.a.a.p.s.PurchaseOrderServiceImpl       : ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=currency, rootBeanClass=class com.axelor.apps.purchase.db.PurchaseOrder$ByteBuddy$VxzRs8gM, messageTemplate='{javax.validation.constraints.NotNull.message}'}
c.a.a.p.s.PurchaseOrderServiceImpl       : ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=company, rootBeanClass=class com.axelor.apps.purchase.db.PurchaseOrder$ByteBuddy$VxzRs8gM, messageTemplate='{javax.validation.constraints.NotNull.message}'}
c.a.a.p.s.PurchaseOrderServiceImpl       : ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=supplierPartner, rootBeanClass=class com.axelor.apps.purchase.db.PurchaseOrder$ByteBuddy$VxzRs8gM, messageTemplate='{javax.validation.constraints.NotNull.message}'}
c.a.a.p.s.PurchaseOrderServiceImpl       : ======================= FETCHING VALIDATION ERRORS============================

这些日志对我来说非常混乱,比如当我记录 currency ,它的值被记录,所以它显然不是空的。
但我得到了同一字段的约束冲突异常:(

c.a.a.p.s.PurchaseOrderServiceImpl       : CURRENCY = Currency{id=64, name=Indian Rupee, code=INR, symbol=₹}

c.a.a.p.s.PurchaseOrderServiceImpl       : ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=currency, rootBeanClass=class com.axelor.apps.purchase.db.PurchaseOrder$ByteBuddy$VxzRs8gM, messageTemplate='{javax.validation.constraints.NotNull.message}'}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题