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.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论