问题描述
我在Hibernate中使用了每个层次结构的类策略"来映射以下类层次结构.
I have used a "class per hierarchy strategy" for mapping the following class hierarchy in Hibernate.
我有 Item 作为抽象类.具体如下:图书日记 DVD CD
I have Item as an abstract class.The followings as concrete ones:BookJournalDVDCD
我的映射非常适合保存和加载,但是在更新具有不同类的项目时无法正常工作.我在上述每个类中都使用了区分符.
My mapping works perfect for saving and loading but it doesn't work correctly on updating an item with a different class.I'm using discriminators for each of the above classes.
我的item.hbm.xml就像:
my item.hbm.xml is like:
<!-- language: lang-xml -->
<hibernate-mapping>
<class name="my.hibernate.items.Item" discriminator-value="item"
table="testitem" catalog="efeu">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="native" />
</id>
<discriminator column="item_type" type="string" length="8" />
<subclass name="my.hibernate.items.DVD" discriminator-value="DVD">
<property name="duration" type="java.lang.Integer" column="duration" />
<property name="trackNumber" type="java.lang.Integer"
column="trackNumber" />
</subclass>
<subclass name="my.hibernate.items.Journal"
discriminator-value="Journal">
<property name="serialNumber" type="java.lang.Integer"
column="serial_number" />
</subclass>
</class>
</hibernate-mapping>
对于更新,我使用此方法:
and for updating I use this method:
<!-- language: lang-java -->
public static void updateItem(Item newItem){
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.update(newItem);// I tried merge too. it created a new row
tx.commit();
session.close();
}
我将哪个类实例发送到updateItem方法没有区别.除区分符外,所有字段都将更新,因此,如果它是一本书,那么它仍然是一本书,因此我无法将其以此类对象作为参数调用上述方法,将其转换为CD,DVD或日记.
There is no difference which class instance I send to updateItem method. All the fields are updated except the discriminator so if it is a book remains a book and I can't convert it to CD, DVD or journal invoking the above method with an object of that kind as parameter.
我认为我在做什么很明显.我假设我知道数据库表中id = 111的行是Book对象.因此,我执行以下操作:
I think it is clear what I am doing. I assum that I know that the row with id=111 in the database table is a Book object. So I do the following:
<!-- language: lang-java -->
Item cd = new CD();
cd.setId(111);
cd.setXXXProperties()
Item.updateItem(cd);
很明显,在调用该方法之前cd是瞬态的,在此之前我还没有加载id = 111的行.
it is clear that cd is transient before invoking the method and I haven't loaded the row with id=111 before that.
在调用更新方法时,如何使Hibernate更改discriminator列?
What should I do to make Hibernate change the discriminator column on invoking the update method?
推荐答案
无法更新discriminator
值.您所遇到的是正确的.一旦创建了对象(作为CD或书籍),它就不会更改其类型.
There is no way how to update a discriminator
value. What you are experiencing is correct. Once object is created (as a CD or as a Book) it won't change its type.
在此处了解更多信息: 5.1 .6.1.1.鉴别符
Read more here: 5.1.6.1.1. Discriminator
<discriminator
column="discriminator_column" (1)
type="discriminator_type" (2)
force="true|false" (3)
insert="true|false" (4)
formula="arbitrary sql expression" (5)
/>
没有针对鉴别器的更新操作.
There is no update operation for discriminator.
这篇关于当我用另一个对象更新时,hibernate不会更新鉴别符列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!