2014-08-29 2 views
2

내 최대 절전 모드 엔티티에 대한 기본 클래스의 몇 가지있다 :MappedSuperClasses에서 속성 값을 무시하고 최대 절전 모드로 전환 하시겠습니까?

@MappedSuperclass 
public abstract class Entity<T> { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private T id; 

    public T getId() { return this.id; } 
} 

@MappedSuperclass 
public abstract class TimestampedEntity<T> extends Entity<T> { 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "created_at") 
    private Date createdAt; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "updated_at") 
    private Date updatedAt; 

    // Getters and setters for createdAt and updatedAt.... 
} 

각 엔티티 클래스 분명히 이러한 기본 클래스를 확장하고 ID 속성과 createdAt 및 updatedAt 특성을 얻을 수 ...

@Entity 
public class SomeEntity extends TimestampedEntity<Long> { 
    // Insert SomeEntity's fields/getters/setters here. Nothing out of the ordinary. 
} 

내 문제는 SomeEntity의 새로운 인스턴스를 저장할 때 Hibernate가 수퍼 클래스에서 속성 값을 무시하고 있다는 것이다. id 열 (테이블의 ID 열과 동기화되지 않음)에 대한 자체 값을 삽입하려고 시도하고 이 확실히으로 설정 되었더라도 createdAt 및 updatedAt에 대해 null을 삽입하려고 시도합니다.

SomeEntity e = new SomeEntity(/* ... */); 
e.setCreatedAt(new Date()); 
e.setUpdatedAt(new Date()); 

// Here Hibernate runs an INSERT statement with nulls for both createdAt and updatedAt 
// as well as a wildly out of sequence value for id 
// I don't think it should be trying to insert id at all, since the GenerationType is IDENTITY 
Long id = (Long)session.save(e); 

여기서 내가 뭘 잘못하고 있니? 어떻게하면 Hibernate가 MappedSuperclasses로부터 프로퍼티 값을 가져 오게 할 수 있습니까?

답변

2

이 필드는 비공개로 선언되어 있습니다. 그것이 실제로 당신이 의도하는 것이라면 (보호되거나 패키지로 보호되는 것과는 대조적으로), 주석을 게터로 이동하십시오.

getters에서 주석을 갖는 것은 최대 절전 모드의 모범 사례로 정의됩니다. 참조 : Hibernate Annotation Placement Question

SomeEntity의 인스턴스는 해당 필드가 수퍼 클래스의 개인 구성원이기 때문에 실제로 'updatedAt'필드가 없습니다. 지속성 공급자는 수퍼 클래스가 해당 필드에 대한 열을 필요로한다는 것을 알지만 구체적인 클래스가 'updateAt'필드를 '볼 수 없으므로 인스턴스화 된 구체 클래스가 해당 열을 사용하지 않는 것으로 간주합니다.

관련 문제