2013-03-11 24 views
1

我有以下情形:@MappedSuperclass超類屬性是做後一空的選擇

我有有一個String屬性「A」,並從映射超類擴展子類@Entity一個超類和有另一個字符串屬性B.

我不需要做刀片,我只需要做選擇了表B.

但是,當我查詢B級,休眠加載所有B中的屬性,但,映射的超類屬性不會被加載,所以,如果我做了.getPropertyB()包含正確的值,但是,如果我做了B.getPropertyA()方法返回null。

的類看起來是這樣的:

@MappedSuperclass 
public abstract class BaseContributorEntity extends BaseEntity<Long> implements Comparable<BaseContributorEntity> { 
    private String propertyA; 

    @Column(name = "column_a", length = 450) 
    public String getPropertyA() { 
     return propertyA; 
    } 

    public void setPropertyA(String value) { 
     this.propertyA = value; 
    } 
} 

@Entity 
@Table(name = "work_contributor") 
public class WorkContributorEntity extends BaseContributorEntity { 
    .... Other properties, including @id .... 

    private String propertyB; 

    @Column(name = "column_b", length = 450) 
    public String getPropertyB() { 
     return propertyB; 
    } 

    public void setPropertyB(String value) { 
     this.propertyB = value; 
    } 

    .... Other setters and getters .... 

} 

難道有人知道爲什麼會出現這種情況?

我是否應該重載映射超類A的getters?

我與Hibernate 3.6.3和JPA 2.0

回答

1

我已經用@MappedSuperclass工作多年,效果很好。你的代碼示例是正確的。請仔細檢查您的真實代碼,這應該是一些手工錯誤。

+1

我同意這一點。但這不應該成爲一個評論嗎? – 2013-03-11 16:29:07

+0

我更新了類名和註解,你看到有可能的錯誤嗎? – user1739166 2013-03-11 16:47:11

+0

我查看了你的代碼。這似乎是正確的。 您是否在BaseEntity類中添加了@MappedSuperclass?如果你想映射它的屬性,你應該這樣做。 您是否可以通過單元測試創​​建一個WorkContributorEntity實例,並使用*** propertyA ***填充,如果有效的話。這意味着它的O/R映射是正確的。 – 2013-03-12 11:58:19