문제

내가 원하는 것 :

@Embedded(nullable = false)
private Direito direito;

그러나 아시다시피 @embeddable에는 그러한 속성이 없습니다.

이 작업을 수행하는 올바른 방법이 있습니까? 나는 해결 방법을 원하지 않습니다.

도움이 되었습니까?

해결책

임베드 가능한 구성 요소 (또는 복합 요소, 호출하려는 모든 것)에는 일반적으로 둘 이상의 속성이 포함되어 있으므로 둘 이상의 열에 매핑됩니다. 따라서 전체 구성 요소는 널리 이루어집니다. 따라서 다른 방식으로 처리 될 수 있습니다. J2EE 사양은 어떤 식 으로든 지시하지 않습니다.

최대 절전 모드는 모든 속성이 무인 상태 인 경우 구성 요소를 무효로 간주합니다 (그리고 그 반대). 따라서 속성 중 하나 (모든)가 무효가되지 않도록 선언 할 수 있습니다 ( @Embeddable 또는의 일부로 @AttributeOverride ~에 @Embedded) 원하는 것을 달성합니다.

또는 Hibernate Validator를 사용하는 경우 부동산에 주석을 달 수 있습니다. @NotNull 이로 인해 DB 수준이 아닌 앱 레벨 점검 만 초래할 수 있습니다.

다른 팁

Hibernate 5.1 이후 "Hibernate.create_empty_composites.enabled"를 사용할 수 있습니다. https://hibernate.atlassian.net/browse/hhh-7610 )

@embeddable로 표시된 클래스에 더미 필드를 추가하십시오.

@Formula("0")
private int dummy;

보다 https://issues.jboss.org/browse/hibernate-50 .

나는 이전에 한 제안 중 하나에 너무 흥분하지 않았으므로 나를 위해 이것을 처리 할 수있는 측면을 만들었습니다.

이것은 완전히 테스트되지 않았으며, 내장 된 물체의 컬렉션에 대해 확실히 테스트되지 않으므로 구매자 뷰어. 그러나 지금까지 나를 위해 일하는 것 같습니다.

기본적으로, getter를 가로 채 웁니다 @Embedded 필드 및 필드가 채워 졌는지 확인합니다.

public aspect NonNullEmbedded {

    // define a pointcut for any getter method of a field with @Embedded of type Validity with any name in com.ia.domain package
    pointcut embeddedGetter() : get( @javax.persistence.Embedded * com.company.model..* );


    /**
     * Advice to run before any Embedded getter.
     * Checks if the field is null.  If it is, then it automatically instantiates the Embedded object.
     */
    Object around() : embeddedGetter(){
        Object value = proceed();

        // check if null.  If so, then instantiate the object and assign it to the model.
        // Otherwise just return the value retrieved.
        if( value == null ){
            String fieldName = thisJoinPoint.getSignature().getName();
            Object obj = thisJoinPoint.getThis();

            // check to see if the obj has the field already defined or is null
            try{
                Field field = obj.getClass().getDeclaredField(fieldName);
                Class clazz = field.getType();
                value = clazz.newInstance();
                field.setAccessible(true);
                field.set(obj, value );
            }
            catch( NoSuchFieldException | IllegalAccessException | InstantiationException e){
                e.printStackTrace();
            }
        }

        return value;
    }
}

NullSafe Getter를 사용할 수 있습니다.

public Direito getDireito() {
    if (direito == null) {
        direito = new Direito();
    }
    return direito;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top