These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NHibernate and "inserting to update"
PostPosted: Thu Jun 11, 2009 2:31 am 
Newbie

Joined: Sun May 17, 2009 2:14 pm
Posts: 4
I'm working with a database that has a lot of "versioned" tables like this:
Code:
CREATE TABLE Person (
  Person_ID INT
)

CREATE TABLE PersonInfo (
  Person_ID INT,
  Version INT,
  FirstName VARCHAR(50),
  LastName VARCHAR(50)
)

The idea is that we never actually update a person; instead, any time a person's information changes, we insert a new row into the PersonInfo table with an incremented Version. We also have views (e.g., vwPerson_LatestVersion) which return the most recent version for each person.

I don't want this versioning business to exist in my object model because it hardly ever matters. My solution has been to map classes to the *_LatestVersion views and provide custom sql-insert and sql-update code to perform the acrobatics to insert and update people. For example:
Code:
<sql-update>
DECLARE
  @Version AS INT = ?,
  @FirstName AS INT = ?,
  @LastName AS INT = ?,
  @Person_ID AS INT = ?

INSERT INTO PersonInfo ( Person_ID, Version, FirstName, LastName )
VALUES
(
  @Person_ID,
  ( SELECT MAX(Version) + 1 FROM PersonInfo WHERE Person_ID = @Person_ID ),
  @FirstName,
  @LastName
)
</sql-update>

Everything is working perfectly, but it feels dirty.

The things I'm interested in are:

1) Is there a way to use named parameters rather than relying on the order in which NHibernate decides to pass them?
2) Is there a way to do this programatically, perhaps by implementing my own IEntityPersister? (That interface is ginormous!)
3) Is there an option that I'm just missing?

Any feedback would be very appreciated!

Thanks

- chad


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.