开发者

JPA, transient annotation not overwriting OneToOne?

I have a superclass Product and a subclass Reduction. Now I want the subclass to override a property of the superclass so it isn't persisted in the database. I excpeted it to work like this but apparently hibernate still tries to store the ProductType property of the Reduction in the database. Is there another way to fix this ?

@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();
    }
}

And the subclass:

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

    }
}


You are looking for

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

    }
}


I solved it using a workaround, in Reduction I put

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

And in class Product:

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

It's ugly but so far this is the only option that works. Starting to wonder if the original question is a bug in hibernate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜