2014-02-19 1 views
1

Grails에서 Spring 보안 코어 플러그인은 사용자 및 역할 도메인을 생성하는 데 도움이됩니다.UserRole이 Serializable을 구현하고 equals() 및 hashcode() 메소드를 대체하는 이유

UserRole.groovy 

import org.apache.commons.lang.builder.HashCodeBuilder 

class UserRole implements Serializable { 

    private static final long serialVersionUID = 1 

    User user 
    Role role 

    boolean equals(other) { 
     if (!(other instanceof UserRole)) { 
      return false 
     } 

     other.user?.id == user?.id && 
      other.role?.id == role?.id 
    } 

    int hashCode() { 
     def builder = new HashCodeBuilder() 
     if (user) builder.append(user.id) 
     if (role) builder.append(role.id) 
     builder.toHashCode() 
    } 

    static UserRole get(long userId, long roleId) { 
     UserRole.where { 
      user == User.load(userId) && 
      role == Role.load(roleId) 
     }.get() 
    } 

    static UserRole create(User user, Role role, boolean flush = false) { 
     new UserRole(user: user, role: role).save(flush: flush, insert: true) 
    } 

    static boolean remove(User u, Role r, boolean flush = false) { 

     int rowCount = UserRole.where { 
      user == User.load(u.id) && 
      role == Role.load(r.id) 
     }.deleteAll() 

     rowCount > 0 
    } 

    static void removeAll(User u) { 
     UserRole.where { 
      user == User.load(u.id) 
     }.deleteAll() 
    } 

    static void removeAll(Role r) { 
     UserRole.where { 
      role == Role.load(r.id) 
     }.deleteAll() 
    } 

    static mapping = { 
     id composite: ['role', 'user'] 
     version false 
    } 
} 

내가 본하거나 직렬화 interface.I를 구현하는 도메인 클래스를 만든 적이는 UserRole는 Serializable를 구현 왜 Grails의 직렬화 과정 internally.So을 처리하는 생각? 그리고 UserRole의 id가 이미 User와 Role에 합성되어 있기 때문에 equals()와 hascode() 메소드를 오버 라이딩하는 것의 이점은 무엇입니까?

답변

1

이것은 합성 키를 사용할 때 Hibernate의 요구 사항이다; http://docs.jboss.org/hibernate/orm/3.5/reference/en-US/html/mapping.html#mapping-declaration-compositeid

+0

UserRole에서 Serializable 인터페이스를 제거하고 해시 코드 메소드를 같게하고 mysql과 mongodb에서 테스트합니다. "org.hibernate.MappingException : composite-id 클래스가 Serializable을 구현해야합니다"라는 최대 절전 모드로 mysql을 사용하면서. mongodb. 그래서 나는 그것이 datasourse가 최대 절전 모드 일 때만 필요하다고 생각한다. 내가 맞다면이 플러그인은 다양한 데이터 소스에 의해 사용되기 때문에 스프링 보안 코어 플러그인 문서에 언급되어야한다. –

+0

그건 그렇고,이 정보를 주셔서 고마워. 난 최대 절전 모드에 대한 지식이없고 난 단지 직렬화에 대해 거의 알지 못한다. 아직도 내 마음 속에 질문이 남아있다. 왜 최대 절전 모드가 Serializable 인터페이스를 구현하는이 조건을 부과 했습니까? –

관련 문제