2011-09-21 43 views
0

我有一个超类产品和一个子类减少。现在我想让子类覆盖超类的属性,所以它不会保留在数据库中。我鼓励它像这样工作,但显然休眠仍尝试将Reduce的ProductType属性存储在数据库中。有其他方法可以解决这个问题吗?JPA,瞬态注释不会覆盖OneToOne?

@Entity 
@Table(name="PRODUCT_INSTANCE") 
public class Product { 
    private Integer id; 
    protected ProductType productType; 

    public Product(){ 

    } 

    public Product(ProductType productType) { 
     this.productType = productType; 
    } 

    @OneToOne 
    public ProductType getProductType() { 
     return productType; 
    } 

    public void setProductType(ProductType type) { 
     this.productType = type; 
    } 

    @Transient 
    public ProductCategory getCategory() { 
     return productType.getCategory(); 
    } 
} 

类和子类:

@Entity 
@Table(name="REDUCTION") 
public class Reduction extends Product { 

    private ProductType type; 
    private Integer id; 

    public Reduction(Double percentage, Double totalPrice) { 

     ProductCategory reductionCat = new ProductCategory("_REDUCTION_", new Color("", 130, 90, 80)); 
     type = new ProductType(); 
     type.setBuyPrice(0.0); 
     type.setBtw(BTW.PER_0); 
     type.setCategory(reductionCat); 
    } 

    @Override 
    @Transient 
    public ProductType getProductType() { 
     return type; 

    } 
} 

回答

0

您正在寻找

@Entity 
@Table(name="REDUCTION") 
@AttributeOverride(name = "productType", column = @Column(name = "productType", nullable = true, insertable = false, updatable = false)) 
public class Reduction extends Product { 
    @Override 
    public ProductType getProductType() { 
     return type; 

    } 
} 
+0

没有,实际上没有做我想要的,AttributeOverride只是让休眠保存productType在不同的列。我想要的是,reduce类没有保存productType。 –

+0

看看代码 – ssedano

+0

那么即使有了这些修改,hibernate仍然抱怨说,它不能保存ProductType,因为它是一个临时对象(当它试图持久化Reduce对象时)。 –

0

我解决它使用一种变通方法,以减少我把

@Transient 
public ProductType getProductHack(){ 
    return type; 
} 

,并在类产品:

@OneToOne 
public ProductType getProductType() { 
    return productType; 
} 

public void setProductType(ProductType type) { 
    this.productType = type; 
} 

@Transient 
public ProductType getProductHack() { 
    return getProductType(); 
} 

public void setProductHack(ProductType type) { 
    setProductType(type); 
} 

这是丑陋的,但到目前为止,这是唯一可行的选择。开始怀疑原始问题是否是休眠时的错误。