开发者

Convert type of entity to subtype with NHibernate/Hibernate

Would you please help me to find out better solution for the following issue... There is an hierarchy at my domain model:

class Document { 
  Int Id; 
  String Title; 
}
class Agreement : Document { 
  DateTime Signed; 
}

Persistent strategy is Table-per-class. I.e. 2 tables: 'Documents' (Id, ...) and 'Agreements' (Fk_document, ...)

First operation is the registration of document. At this moment I don't know document's real type. So I create new Document and fill its property 'Title'.

After some time I receive additional information. According with it the previously registered document actually was an agreement which was signed at some date. Now I can fill property 'Signed' of Agreement.

But to do this it is necessary to somehow convert type from Document to Agreement. I could make SQL-call like this:

INSERT INTO AGREEMENTS (FK_DOCUMENT, S开发者_如何学GoIGNED_DATE) VALUES ( 1111111, TO_DATE('01-01-2000') ) 

(Where '1111111' is Id which Hibernate assign to document while saving it first time)

I believe it is not best way at all! But I couldn't find any others... Thank you!

(I thought up this example, real use-case is rather difficult and I don't want to encumber the question. Documents must be saved as soon as possible)


When you need to 'change the class' of a persisted entity it's a sign that you don't really want to model this as a class hierarchy. Instead, use composition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜