I am writing a hibernate wrapper around a client's database. This one particular table does not have a primary key.
This is the SQL that created the table
CREATE TABLE [CONFIG] (
[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Value] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[LastModified] [datetime] NULL
) ON [PRIMARY]
GO
I used Middlegen to create my mapping file and this is what it come up with...
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="com.unveil.bizobjs.lecstar.hibernate.Config"
table="CONFIG"
>
<composite-id>
<key-property
name="name"
column="Name"
type="java.lang.String"
length="50"
/>
<key-property
name="value"
column="Value"
type="java.lang.String"
length="50"
/>
<key-property
name="lastModified"
column="LastModified"
type="java.sql.Timestamp"
length="23"
/>
</composite-id>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->
</class>
</hibernate-mapping>
Middlegen has automatically created a composite primary key comprised of every row in the table.
After much debugging and searching I have discovered that Hibernate won't allow me to update the fields in a composite primary key.
So you may ask why don't I add an ID column to the table. Well, the schema is not really up for change. I need to make it work witht he existing one.
You may also ask how I can select a for update when there is no primary key. Glad you asked. My client has given me this query statement that select's the row that needs to be updated...
Query query = session.createSQLQuery("select top 1 {config.*} from Config config where config.name like 'CreditcardUser%' and (DateDiff(\"n\", config.lastModified, getdate()) >= 3 or config.value = '') and config.value <> 'PCCHARGE'", "config", Config.class);
Apparently this selects a row unique enough for their needs
So my question is what can I do to update one of these rows?
I'm using Hibernate 2.1 with SQL Server 2000 sp3a