"Invalid object name 'dbo.hibernate_unique_key'."

273 views
Skip to first unread message

nh_3478

unread,
Aug 20, 2010, 4:03:47 PM8/20/10
to nhusers
I get a genericadoexception with error {"could not get or update next
value[SQL: ]"}
and InnerException: "Invalid object name 'dbo.hibernate_unique_key'."

I am following, to the T, the Nhibernate 2 book by Aaron Cure.


//mssql 2008 express edition with table
OrderHeader
PK = Id, type = int
then the rest of values seen in the constructor below are also there.

//my OrderHeader.hbm.xml file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Ordering.Data" namespace="Ordering.Data" >
<class name="Ordering.Data.OrderHeader, Ordering.Data"
table="OrderHeader" schema="dbo" >
<id name="Id" type="int" >
<column name="Id" />
<!--<column name="Id" length="4" sql-type="int" not-null="true"
unique="true" index="PK_OrderHeader" />-->
<generator class="hilo" >
<!--<param name="max_lo">100</param>-->
</generator>
</id>
<property name="Number" type="String"/>
<property name="OrderDate" type="DateTime"/>
<property name="ItemQty" type="Int32"/>
<property name="Total" type="Decimal"/>
</class>
</hibernate-mapping>


//OrderHeader class snipet...
namespace Ordering.Data
{
public class OrderHeader
{
private int _id;

public virtual int Id
{
get { return _id; }
set { _id = value; }
}
...

When I try to save using code like so, I get the aformentioned errors.

//code
OrderHeader oh = new OrderHeader("10", DateTime.Now, 2,
105, c, null, null, null);
//oh.Id = 51;
session.Save(oh);


Now, I have it boiled down to the hilo generator. If I remove the
generator from the hbm.xml file, and assign a value to oh.Id = 51,
then my object saves and the row is inserted into the database. How
do I get this generator to work, though??? I've also tried native,
but then I get a different error that it can't INSERT into database
using NULL ID...somehow the generator is NOT assigning a valid value
to my ID in my OrderHeader class.

Frans Bouma

unread,
Aug 20, 2010, 4:15:30 PM8/20/10
to nhu...@googlegroups.com
http://www.nhforge.org/doc/nh/en/#mapping-declaration-id-generator

see the snippet below 'hilo'. you have to create a table for the number and
add a row.

FB

> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.

nh_3478

unread,
Aug 20, 2010, 5:20:12 PM8/20/10
to nhusers
Okay, just incase this isn't clear enough for others and to make sure
I understand, I created a new table called HiValue and a PK field
named NextValue and seeded it with a default value of 0. In my hbm
file for OrderHeader I now have:

<generator class="hilo" >
<param name="table">HiValue</param>
<param name="column">NextValue</param>
<!--<param name="max_lo">100</param>-->
</generator>

The max_lo property, is that just to indicate a max value?


Also, all the tables use an ID field, at least in the examples I'm
following, so am I supposed to use a seperate HiValue table for each
table I have? Is that the proper way to ensure proper sequencing of
ID numbers? So, for instance, I have two tables: OrderHeader and
OrderItem; is it best practice to create, say, OrderHeader_HiValue and
OrderItem_HiValue tables and seed them how I see fit (e.g. 0, 100,
etc) for a starting ID value?



btw, thanks for the link...
> >http://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -

Diego Mijelshon

unread,
Aug 20, 2010, 7:57:48 PM8/20/10
to nhu...@googlegroups.com
NextValue should NOT be a PK (it shouldn't be indexed at all), because it will have a single row that will change constantly (in essence, it's simulating a sequence in databases that don't have them)
Reply all
Reply to author
Forward
0 new messages