2017-02-27 3 views
1

에서 열을 추가 주석 나는 class MyLongProperty extends MyAbstractProperty<Long>{} 같은 하위 클래스에 대한 기초 클래스최대 절전 모드 : 상속

@MappedSuperclass 
public abstract class MyAbstractProperty <T extends Object>{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private Long id; 

    @Column(name="defaultValue") 
    protected T defaultValue; 

//... 

} 

있습니다. 이 작동합니다. 클래스 class MyStringProperty extends MyAbstractProperty<String>{}에 텍스트를 저장하려고 할 때 문제가 발생합니다.

기본 최대 절전 모드는 최대 255 자 (참조 : JPA: "Data too long for column" does not change)의 문자열 만 저장한다는 것을 알게되었습니다. 따라서 내 defaultValue 같은 부가적인 주석을 필요 :

@Entity 
class MyStringProperty extends MyAbstractProperty<String>{ 

    @Type(type="org.hibernate.type.StringClobType") 
    @Column(name="defaultValue") 
    protected String defaultValue; // problem since defaultValue is already defined in MyAbstractProperty<>. 
} 
DEFAULTVALUE가 이미 MyAbstractProperty에 정의되어 있기 때문에

어떻게이를 보장 할 수 있는가? MyLongProperty과 같은 다른 클래스에 부작용이있을 수 있으므로 형식 주석을 MyAbstractProperty으로 옮기는 것이 좋다고 생각하지 않습니다. 예를 들어

내가 MyAbstractProperty@Lob을 설정했다 MyStringProperty의 SQL 정의는 DEFAULTVALUE BLOB(255) 대신 DEFAULTVALUE BIGINT 포함되어 있습니다.

추가 질문 : 텍스트 데이터로 열을 정의하는 옵션이 다른 것 같습니다 (예 : @Type(type="org.hibernate.type.StringClobType"), @Lob).이 옵션의 차이점은 어디에 있습니까? 가장 잘하는 것. (주 데이터베이스는 HyperSQL이지만 SQLite, MariaDB 및 MySQL도 대상으로합니다.)

솔루션이 추상적 인 게터/세터있을 것 같다 최근의 의견을 바탕으로 편집

. 나는 이것이 내 코드에 어떤 의미가 있는지 확인해야한다.

대체로 아이디어가 없기 때문에 코드가 수정되지 않을 수 있습니다. 이 클래스와

class MyText extendes String; 

같은 MyText의 모든 인스턴스가 자동으로 @Lob로 저장을 정의 할 수 있습니까? MyAbstractProperty<String>MyAbstractProperty<MyText>으로 수정해야합니다.

+0

나는 상속 전략 중 하나를 따라야한다고 생각하기 때문에이 기사를 읽으십시오. http://www.thejavageek.com/2014/ 05/14/jpa-single-table-inheritance-example/ –

+0

그래서 당신은 당신이 추상 클래스에서 원하는 전략으로 @ 상속 주석을 사용해야합니다 –

+0

또한 MyStringProperty에서 defaultValue를 제거해야한다고 생각합니다. 이미 수퍼 클래스에있을 것이고 @Type (type = "org.hibernate.type .StringClobType ") to MyAbstractProperty –

답변

1

당신은 슈퍼 클래스 만 추상적 인 게터를두고 서브 클래스의 필드 정의 할 수 있습니다 :

특별한 치료를 필요로하지 않는 다른 모든 경우에 대한 상용구를 방지하기 위해
@MappedSuperclass 
public abstract class MyAbstractProperty<T extends Object>{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.TABLE) 
    private Long id; 

    public abstract T getDefaultValue(); 
} 

@Entity 
class MyStringProperty extends MyAbstractProperty<String>{ 

    @Type(type="org.hibernate.type.StringClobType") 
    @Column(name="defaultValue") 
    protected String defaultValue; 

    @Override 
    public String getDefaultValue() { 
     return defaultValue; 
    } 
} 

, 당신은 간단하게 만들 수 있습니다

@MappedSuperclass 
public abstract class MyDefaultProperty<T> extends MyAbstractProperty<T extends Object> { 

    @Column(name="defaultValue") 
    protected T defaultValue; 

    @Override 
    public T getDefaultValue() { 
     return defaultValue; 
    } 
} 
+0

Dragan Bozanovic에게 감사드립니다. 내 코드에서 일부 리팩토링이 필요할 수도 있습니다. 친절하게도 내 질문의 편집을 확인하십시오. 어쩌면이 아이디어가 더 간단한 해결책으로 이어질 수 있을까요? – BerndGit

+0

@BerndGit'String'에서 확장 할 수 없습니다. 최종 클래스입니다. 그리고 가능하다하더라도, 그 결과는 포함 된 클래스의'MyText' 타입의 필드에'@ Lob' 주석을 위치시켜야하므로 동일합니다. –

관련 문제