简体   繁体   English

使用静态表休眠一对一映射

[英]Hibernate one to one mapping with Static table

According to my requirement, I have a Rate Master table where some criteria wise rate is defined. 根据我的要求,我有一个Rate Master表,其中定义了一些标准明智的汇率。 This is static table and does not change on transaction basis.Now whenever user register to the system they subscribe for a specific plan(Rate) and in subscription table we store the user details along with with rate wise subscription they have chosen. 这是静态表,不会在交易基础上更改。现在,每当用户向系统注册时,他们就订阅特定的计划(费率),并且在订阅表中,我们将存储用户详细信息以及他们选择的按费率订阅。

**Table Details:**
**Rate Master Table -** 
rate_id- INT -Auto generated.
Amount - INT

**Subscription table -** 
ID - INT -Auto generated.
User_ID - VARCHAR - USER ID (No relation with user table)
Rate_Id - INT - RATE MASTER table ID (Though this only hibernate will fetch all the records of Rate)

Now please help me how this mapping should be done on Hibernate POJO side. 现在请帮助我如何在Hibernate POJO端完成此映射。 For individual table wise pojo everything is working as expected but when I am doing relationship in Subscription pojo, system forcing me to mark insertable=false,updatable=false. 对于单个表明智的pojo,一切都按预期工作,但是当我在Subscription pojo中进行关系时,系统迫使我将insertable = false,updatable = false标记为。 So, please help me with the Subscription table POJO and process to do it without updating rate table in every subscription update. 因此,请帮助我处理订阅表POJO,并在不更新每次订阅更新中的费率表的情况下进行处理。 Note: This relation will be uni-directional subscription table to rate mapping only. 注意:此关系将是单向订阅表到速率映射。

@Column(name = "rate_id",insertable=false,updatable=false)
private int rateId;
@OneToOne(targetEntity = RateMaster.class, cascade = CascadeType.ALL)
@JoinColumn(name = "rate_id", referencedColumnName = "rate_id")
private RateMaster rateMaster;

Thanks in advance. 提前致谢。

Hibernate insists on insertable=false,updatable=false because the rateId field is mapped to the same column that is used as a join column for the relationship with the RateMaster entity. Hibernate坚持使用insertable=false,updatable=false rateId insertable=false,updatable=false因为rateId字段映射到与RateMaster实体的关系用作rateId列的同一列。 You have thus introduced a redundancy, and Hibernate cannot promise to preserve any inconsistent data you introduce through that redundancy. 这样就引入了冗余,并且Hibernate无法保证保留通过该冗余引入的任何不一致的数据。

My recommendation would be to just ditch the rateId field altogether. 我的建议是完全rateId字段。 Manage the underlying column via the RateMaster entity assigned to each Subscription . 通过分配给每个SubscriptionRateMaster实体管理基础列。 If necessary, introduce a read-only, non-persistent accessor on Subscription that provides the rateId by obtaining it from the RateMaster . 如有必要,在Subscription上引入一个只读的非持久性访问器,该rateId通过从RateMaster获取来提供RateMaster

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM