JPA,瞬态注释不会覆盖OneToOne?

时间:2011-09-21 13:05:14

标签: java jpa annotations

我有一个超类Product和一个子类Reduction。现在我希望子类覆盖超类的属性,因此它不会保留在数据库中。我为此工作而烦恼,但显然hibernate仍然试图在数据库中存储Redu的ProductType属性。还有其他方法可以解决这个问题吗?

@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;

    }
}

2 个答案:

答案 0 :(得分: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;

    }
}

答案 1 :(得分: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);
}

这很难看,但到目前为止,这是唯一有效的选择。开始怀疑原始问题是否是hibernate中的错误。