문제

저는 Hibernate 3.6.7-Final과 Spring 3.0.5를 사용하고 있습니다.

나는 이런 엔터티를 가지고 있습니다

@Entity
public class Foo {
    @ManyToOne
    private Bar bar;
}

@Entity
public class Bar {
    @Column
    String group;
}

어떻게 사용할 수 있나요? @Filter 내가 모든 것을 얻고 싶은 Foo에서 Foo그게 있어요 Bar ~와 함께 group = :group?이는 보안 제약으로 간주됩니다.

방금 설정을 시도했습니다. @Filter(name = "groupFilter", condition = "group = :group") 속성에서 bar ~에서Foo 하지만 작동하지 않았습니다.최대 절전 모드가 이를 지원합니까, 아니면 필터가 엔터티/컬렉션 수준에서만 작동합니까?이 보안 제약을 추가하려면 모든 HQL을 변경해야 합니까?

도움이 되었습니까?

해결책

먼저 @FilterDef를 ​​어딘가에 생성해야 합니다(이것은 사용 가능한 매개변수와 기본 조건을 정의합니다). 그런 다음 특정 클래스에 @Filter를 정의합니다.

마지막으로 세션 개체에서 필터를 활성화하고 필요한 매개변수를 설정해야 합니다.기본적으로 최대 절전 모드 세션에서는 필터가 활성화되지 않습니다.세션이 열리면 원하는 특정 항목을 활성화해야 합니다.

예제는 섹션 19.1을 참조하세요. http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/filters.html

@FilterDef(name="groupFilter", parameters={@ParamDef( name="group", type="string" )})
@Filters(
  { @Filter(name="groupFilter", condition="group = :group") }
)
public class ... {
}

그런 다음 dao 코드 어딘가에:

session.enableFilter("groupFilter").setParameter("group", group);

hql을 건드릴 필요가 없습니다.필터를 활성화하면 실제 @Filter가 정의된 모든 클래스가 자동으로 조건을 적용합니다.

컬렉션에 대한 작업을 수행하는 추가 방법이 있으며 이에 대해서는 위에 참조된 문서를 읽어 보시기 바랍니다.그러나 일반적으로 @Filter 주석 클래스와 컬렉션 속성을 제공할 수 있습니다.

다른 팁

필터를 사용할 때의 문제 @ManyToOne Join은 다음에서 옵니다.나는 이것이 내가 살펴보아야 할 것이기 때문에 Hibernate 4.3.10을 언급하고 있다.

관련 SQL 조각은 클래스에 의해 생성됩니다. org.hibernate.engine.internal.JoinSequence, 방법 toJoinFragment:

/**
 * Generate a JoinFragment
 *
 * @param enabledFilters The filters associated with the originating session to properly define join conditions
 * @param includeAllSubclassJoins Should all subclass joins be added to the rendered JoinFragment?
 * @param withClauseFragment The with clause (which represents additional join restrictions) fragment
 * @param withClauseJoinAlias The
 *
 * @return The JoinFragment
 *
 * @throws MappingException Indicates a problem access the provided metadata, or incorrect metadata
 */
public JoinFragment toJoinFragment(
        Map enabledFilters,
        boolean includeAllSubclassJoins,
        String withClauseFragment,
        String withClauseJoinAlias) throws MappingException {

        ...

        final String on = join.getAssociationType().getOnCondition( join.getAlias(), factory, enabledFilters, treatAsDeclarations );

정의된 관계에 따라 join.getAssociationType() 보고 CollectionType 또는 EntityType.

전자는 다음과 같은 선언을 의미합니다.

@OneToMany
private List<MyEntity> myEntities;

후자는 다음을 나타냅니다.

@ManyToOne
private MyEntity myEntity;

첫 번째 경우에는 다음 방법이 사용됩니다.

@Override
public String getOnCondition(
        String alias,
        SessionFactoryImplementor factory,
        Map enabledFilters,
        Set<String> treatAsDeclarations) {
    return getAssociatedJoinable( factory ).filterFragment( alias, enabledFilters, treatAsDeclarations );
}

두 번째 경우 방법은 다음과 같습니다.

@Override
public String getOnCondition(
        String alias,
        SessionFactoryImplementor factory,
        Map enabledFilters,
        Set<String> treatAsDeclarations) {
    if ( isReferenceToPrimaryKey() && ( treatAsDeclarations == null || treatAsDeclarations.isEmpty() ) ) {
        return "";
    }
    else {
        return getAssociatedJoinable( factory ).filterFragment( alias, enabledFilters, treatAsDeclarations );
    }
}

이는 다음을 의미합니다.

  • 엔티티의 정의에서 참조 된 하위 엔터티 목록이 있으면 필터가 무조건 적용됩니다.
  • 단일 참조 하위 엔티티가있는 경우 특정 조건이 충족 될 때 필터가 적용됩니다.

코드에 따르면 다음과 같은 경우에 필터를 적용할 수 있다고 생각합니다. @ManyToOne 다음과 같은 경우의 관계:

  • 참조 엔티티에서 우리는 기본 키가 아닌 필드를 사용하거나
  • TREAT 연산자가 사용됩니다(예를 들어 다음을 참조하세요. 여기 또는 여기)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top