-->
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.  [ 2 posts ] 
Author Message
 Post subject: Trouble creating index on foreign key of many-to-one relatio
PostPosted: Tue Mar 30, 2010 12:26 pm 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
I'm using Hibernate 3.3.2 connected to SQL Server Express 2008 and have Hibernate create my schema for me based on the mapping. I use the 'org.hibernate.tool.hbm2ddl.SchemaValidator' facility to do that.

When I connect to an empty DB, Hibernate nicely creates my schema, but I can't seem to get it to create indexes on my foreign keys in my 'many-to-one' collections. I creates the foreign key, but no index.

Can I get Hibernate to create this index for me?

Hibernate version:
3.3.2
Mapping documents:
Abbreviated class mapping file follow:
Code:
<hibernate-mapping package="xxxxxx">

<class name="AssessmentResult" table="ASSESSRESULT">

   <id name="key" column="resultKey">
      <generator class="native"/>
   </id>
   
   <property name="Name" type="string" column="name" length="128"/>
   
   <many-to-one name="Client"
      column="clientKey"
      class="Client"
      not-null="true"
      index="fk_clientKey"
      />
</class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: Trouble creating index on foreign key of many-to-one relatio
PostPosted: Wed Mar 31, 2010 11:45 am 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
We use two databases - MySQL and SQLServer. This is only a problem in SQLServer. MySQL (as of 4.1) creates indexes on foreign key constraints automatically which is what I want.

However, the problem is solved in SQLServer by using the 'database-object' structure in the mapping file as follows:
Code:
<hibernate-mapping>
    ...
    <database-object>
        <definition class="MyTriggerDefinition"/>
        <dialect-scope name="org.hibernate.dialect.Oracle9iDialect"/>
        <dialect-scope name="org.hibernate.dialect.Oracle10gDialect"/>
    </database-object>
</hibernate-mapping>

or:
Code:
<hibernate-mapping>
    ...
    <database-object>
        <create>ALTER TABLE xx ADD INDEX yyy</create>
        <drop></drop>
    </database-object>
</hibernate-mapping>

Perfect.


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