简体   繁体   English

带 getter 但不带 setter 的休眠实体属性:PropertyNotFoundException

[英]Hibernate entity attribute with getter, but without setter: PropertyNotFoundException

I would like to have a entity attribute that hibernate saves to a database, but does not try to set when it reconstructs the object.我想要一个实体属性,它可以在休眠状态下保存到数据库中,但在重建对象时不会尝试设置。

I have a class like this;我有一堂这样的课;

@Entity
class Quote {
    private int itemCost;
    private int quantity;

    public Quote(int itemCost, int quantity) {
        this.itemCost = itemCost;
        this.quantity = quantity;
    }

    public void setItemCost(int itemCost) {
        this.itemCost = itemCost;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getItemCost() {
        return this.itemCost;
    }

    public int getQuantity() {
        return this.quantity;
    }

    // This attribute "totalCost" has a getter only, no setter. 
    // It causes a runtime error (see below). 
    public int getTotalCost() {
        return this.itemCost * this.quantity;
    }
}

I would like the following database table;我想要以下数据库表;

quotes
itemCost   | quantity    | totalCost
------------------------------------
100        | 7           | 700
10         | 2           | 20
6          | 3           | 18

As you can see, the field "totalCost" can be taken from getTotalCost() , but I do not want to have a setTotalCost() method, in my application it would make no sense.如您所见,“totalCost”字段可以从getTotalCost() ,但我不想使用setTotalCost()方法,在我的应用程序中它没有任何意义。

The reason that I would like a field written to a database that is not set again, is so this value is available to other applications that share the database (namely, the graphical interface).我希望将字段写入未再次set的数据库的原因是,此值可用于共享数据库的其他应用程序(即图形界面)。

Obviously, at runtime I currently get this error:显然,在运行时我目前收到此错误:

org.hibernate.PropertyNotFoundException: Could not find a setter for property totalCost in class Quote

I could have an empty setter, but this is unclean.我可以有一个空的二传手,但这是不干净的。 In my real code there are about 13 "read only" attributes like this, I don't want 13 blank setters cluttering up my code.在我的实际代码中,大约有 13 个这样的“只读”属性,我不希望 13 个空白设置器使我的代码变得混乱。

Is there an elegant solution to this?有没有优雅的解决方案?

See Does Hibernate always need a setter when there is a getter?请参阅当有 getter 时,Hibernate 是否总是需要一个 setter? :

On the class use在课堂上使用

@Entity(access = AccessType.FIELD) and annotate your attributes. @Entity(access = AccessType.FIELD) 并注释您的属性。

or或者

You can use the @Transient annotation to mark the field that it shouldn't be stored in the database.您可以使用@Transient 批注来标记不应存储在数据库中的字段。 You can even use the @Formula annotation to have Hibernate derive the field for you (it does this by using the formula in the query it sends to the database).您甚至可以使用 @Formula 注释让 Hibernate 为您派生字段(它通过在发送到数据库的查询中使用公式来实现这一点)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate - PropertyNotFoundException:找不到 - Hibernate - PropertyNotFoundException: Could not find a getter for org.hibernate.PropertyNotFoundException:无法找到0的setter - org.hibernate.PropertyNotFoundException: Could not find setter for 0 HIbernate映射异常:PropertyNotFoundException:找不到setter - HIbernate Mapping Exception: PropertyNotFoundException: Could not find a setter 休眠:PropertyNotFoundException:找不到XXX的吸气剂 - Hibernate: PropertyNotFoundException: Could not find a getter for XXX org.hibernate.PropertyNotFoundException:找不到用于的吸气剂 - org.hibernate.PropertyNotFoundException: Could not find a getter for 无需设置器/获取器即可直接休眠地图成员的字段 - hibernate map member's field directly without setter/getter 使用没有setter的getter - Using a getter without a setter Hibernate找不到属性的setter(org.hibernate.PropertyNotFoundException) - Hibernate doesn't find setter for a property (org.hibernate.PropertyNotFoundException) 在没有名为“类型”的属性的类中,我得到org.hibernate.PropertyNotFoundException:在类中找不到属性类型的设置方法 - In class without a property called “type” I get org.hibernate.PropertyNotFoundException: Could not find a setter for property type in class org.hibernate.PropertyNotFoundException:在类Address中找不到customerId的获取方法 - org.hibernate.PropertyNotFoundException: could not find getter for customerId in class Address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM