-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate and SQLServer uniqueidentifier problem
PostPosted: Thu Nov 27, 2003 12:58 pm 
Newbie

Joined: Mon Oct 06, 2003 10:33 am
Posts: 2
Hello:

Sorry if this topic has been hashed through already, but I haven't been able to find a conclusive answer.

Has anyone gotten Hibernate to work with the SQL Server "uniqueidentifier" column as the primary key? That is, allowing SQL Server to generate the id for you (via the SQL Server "newid"). The problem surfaces when multiple objects are persisted sequentially using the same session. I've tried different generators: native, uuid.hex, assigned.

I believe the problem boils down to the fact that you cannot get the newly generated id once a row is inserted. In effect, "select @@identity" does not work after a new row has been inserted if the primary key is of type uniqueidentifier. Resulting from this is that Hibernate cannot cache the object properly. I receive a "Object has already been loaded error".

Any help on how to get Hibernate and SQL Server working with "uniqueidentifier" would be greatly appreciated.

Thanks in advance.

--Salil


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 10:43 am 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
Hi,

Subclass Hibernate UUIDGenerator and make your own GUID's.

The MS SQL Server GUID format is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX (8-4-4-4-12).

DO NOT generate the brackets shown in MS SQL Server, there ONLY MS compatible (I think).

An alternative to making your own GUID could be done by something like this:

1. Create a GUID table with on column as UID and the other as whatever.
2. In your UUIDGenerator method generate() insert a value to the 'whatever' column in table GUID so a UID is generated.
3. Read the generated UID from table and return as result in method generate()

Kind regards, Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 3:49 pm 
Newbie

Joined: Mon Oct 06, 2003 10:33 am
Posts: 2
andreas_eriksson wrote:
Hi,

Subclass Hibernate UUIDGenerator and make your own GUID's.

The MS SQL Server GUID format is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX (8-4-4-4-12).


But is this approach any different from me using the "assigned" generator and providing my own guid? In other words, using the subclassing approach, won't Hibernate still try to get the id after row insertion, and result in the same problem?

Thanks,
Salil


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 8:38 pm 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
Since the GUID is generated in the app. there's no need for Hibernate performing the 'SELECT @@IDENTITY'.

The uniqueidentifier type in MS SQL Server could still be used, but NOT generated in the db.
The primary key should NOT be defined as type varchar even though we generate the GUIDs strings in the app. The uniqueidentifier type could serve as "validation" in the db. 'Cause if you try to insert a String that's not following the format 8-4-4-4-12 (HEX String), the db will protest.

Regards, Andreas


Top
 Profile  
 
 Post subject: Typ problems with ID of MS SQL 2000
PostPosted: Mon Feb 27, 2006 8:42 am 
Newbie

Joined: Mon Feb 27, 2006 8:25 am
Posts: 3
Hi

I am new to hibernate so my question is probably not that interesting.

My table in the DB uses a own column with automatically assigned GUIDS.
I tried following but it doesn't work.

Thank you very much

the greenhorn

Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: guid, for columns: [org.hibernate.mapping.Column(GUIDModulID)]
Exception in thread "main" java.lang.ExceptionInInitializerError
   at util.HibernateUtil.<clinit>(HibernateUtil.java:17)
   at verwaltung.Modulmanager.createModul(Modulmanager.java:32)
   at verwaltung.Modulmanager.main(Modulmanager.java:17)
Caused by: org.hibernate.MappingException: Could not determine type for: guid, for columns: [org.hibernate.mapping.Column(GUIDModulID)]
   at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
   at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:193)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:982)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1146)
   at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
   ... 2 more



Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="verwaltung.Modul" table="Modul">
      <id name="id" column="GUIDModulID" >
         <generator class="guid"></generator>
      </id>
      <property name="Name"> <column name="ModulBezeichnung"></column></property>
   </class>
</hibernate-mapping>


Code:
package verwaltung;
import org.hibernate.id.GUIDGenerator;

public class Modul {

   private GUIDGenerator id;
   
   private String fName;
   
   public Modul(){}

   public String getName() {
      return fName;
   }

   public void setName(String name) {
      fName = name;
   }

   public GUIDGenerator getId() {
      return id;
   }

   private void setId(GUIDGenerator id) {
      this.id = id;
   };
   
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.