-->
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.  [ 3 posts ] 
Author Message
 Post subject: Updating tables without without a primary key...
PostPosted: Thu Apr 14, 2005 5:44 pm 
Newbie

Joined: Thu Apr 14, 2005 5:19 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 17, 2005 3:04 am 
Newbie

Joined: Tue Apr 05, 2005 8:01 am
Posts: 12
To answer your question logically, you have to override the hashCode() and equals() method in you class, by putting the bussiness logic, i mean the composite id's that you will be having.

To find the record, you should execute a named query specifying all the values for each of the composite-keys.

Ideally you should have a seperate class which represents the composite-key.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 11:41 am 
Newbie

Joined: Thu Apr 14, 2005 5:19 pm
Posts: 2
Is this the same as a "synthetic key" that I have read about in the book "Hibernate in Action?" If so do you know where I can find more documentation on synthetic keys? That book doesn't go into much detail.

Thanks


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

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.