Hibernate 中每个具体类层次结构的表
我有以下 Hibernate 映射,必须使用每个具体类层次结构的表进行映射:
<hibernate-mapping package='dao'>
<meta attribute='class-description'></meta>
<class name='PropertyDAO'>
<id name='id' column='id_property'>
<generator class='assigned'/>
</id>
<property name='address' column='address' type='string'/>
<union-subclass name='HouseDAO' table='house'>
<property name='noOfRooms' column='noOfRooms'/>
<property name='totalArea' column='totalArea'/>
<property name='price' column='price'/>
</union-subclass>
<union-subclass name='LandDAO' table='land'>
<property name='area' column='area'/>
<property name='unitPrice' column='unitPrice'/>
</union-subclass>
</class>
</hibernate-mapping>
这意味着在数据库中我只有 2 个表:
- house (id_property(PK), address, noOfRooms,totalArea, Price)
- land (id_property( PK)、地址、区域、单位价格)
据我了解,在这种情况下,需要在调用 .save() 之前显式生成 ids,所以我的问题是:如何创建自动生成 ids 的策略,以便来自具体类的 id 在连接时形成一个连续的域。
I have the following Hibernate Mapping, which has to be mapped using the Table per Concrete Class Hierarchy:
<hibernate-mapping package='dao'>
<meta attribute='class-description'></meta>
<class name='PropertyDAO'>
<id name='id' column='id_property'>
<generator class='assigned'/>
</id>
<property name='address' column='address' type='string'/>
<union-subclass name='HouseDAO' table='house'>
<property name='noOfRooms' column='noOfRooms'/>
<property name='totalArea' column='totalArea'/>
<property name='price' column='price'/>
</union-subclass>
<union-subclass name='LandDAO' table='land'>
<property name='area' column='area'/>
<property name='unitPrice' column='unitPrice'/>
</union-subclass>
</class>
</hibernate-mapping>
Which means in the database i have only 2 tables :
- house (id_property(PK), address, noOfRooms, totalArea, price)
- land (id_property(PK), address, area, unitPrice)
As far as I understood, in this case the ids need to be generated explicitly before calling .save(), so my question is: How can I create a strategy for the automatically generation of the ids, so that the ids from the concrete class form a continuous domain when joined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,数据库中的模型是错误的,因为您在多个相关的表中拥有冗余信息。
每个具体类的表是一种继承模型,它在运行时会出现问题,因为您可能会遇到这样的情况:更新土地的地址,但不更新房屋的地址,而它们是相同的(语义上)。 Iow:放弃这个模型并引入每个子类表,这样你就有一个包含 id 和地址的属性基表和两个带有 PK 的独立表,PK 是属性基的 pk 的 FK,一个是具有房屋特定字段的房屋,另一个是具有土地特定字段的土地。
这会给你带来最少的问题,因为这是将实体类型之间的继承转换为关系表的方法(请参阅 Nijssen/Halpin 的有关 NIAM/ORM 的书籍)
IMHO your model in the DB is wrong as you have redundant information across multiple tables which are related.
Table per concrete class is an inheritance model which gives problems at runtime as you can have the situation where one updates the address of Land but not of House while they're the same (semantically). I.o.w.: drop this model and introduce table-per-subclass, so you have a property base table with id and address and two separated tables with a PK which is an FK to the pk of property base, one is house with the house specific fields, the other is land with the land specific fields.
That will give you the smallest number of problems as it's the way to convert inheritance between entity types to relational tables (see Nijssen/Halpin's books about NIAM/ORM)
我将以下内容与 join-subclass 一起使用,
这应该使子类使用属性表中的 id,因为它依赖于它。 (超级)列指的是子类中对超级的外部引用的列。
I used the following with joined-subclass
This should make subclasses use the ids from the property table since that is reliant on it. (the super) The column refers to the column in the sub class that is a foreign reference to the super.
解决方案是创建另一个表来存储下一个id; 每次用户想要插入新实体时都应该修改该值。 这样,定义域是连续的
The solution is to create another table that stores the next id; this value should be modified each time a user wants to insert a new entity. In this way, the domain is continuous