-->
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.  [ 9 posts ] 
Author Message
 Post subject: hbm2ddl on mysql, updating indexes and create-drop
PostPosted: Tue Feb 17, 2004 1:51 pm 
Beginner
Beginner

Joined: Wed Feb 04, 2004 4:54 am
Posts: 25
Env: Hibernate 2.1.2, Mysql Connector/J 3.0.10-stable

hibernate.hbm2ddl.auto = update
new columns are correctly added.
Every run hibernate try to readd all the indexes (costraint) but the indexes are already there. Is this a bug or I'm missing something?

hibernate.hbm2ddl.auto = create
If the tables already exists hibernate will drop them even if I use only create and not create-drop.
Again: anyone else experiencing this?



Here is an example mapping:
Code:
<?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>
   <class name="mydomain.model.Page" table="pages">
      <id name="id" column="id" type="long">
         <generator class="native"/>
      </id>

      <many-to-one name="site" class="mydomain.model.Site" column="siteid" not-null="true"/>
      <property name="orderId" column="orderid" type="integer" />
      <property name="shortName" column="shortname" type="string" length="32"/>
      <!-- property name="longName" column="longname" type="string" / -->
      <property name="pageType" column="pagetype" type="string" length="16"/>
      <property name="status" column="status" type="integer" not-null="true"/>
      
      <set name="contents" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="pageid"/>
         <one-to-many class="mydomain.model.Content"/>
      </set>

   </class>
</hibernate-mapping>


And the DDL:

Code:
drop table if exists pages
create table pages (
   id BIGINT NOT NULL AUTO_INCREMENT,
   siteid BIGINT not null,
   orderid INTEGER,
   shortname VARCHAR(32),
   pagetype VARCHAR(16),
   status INTEGER not null,
   primary key (id)
)
alter table pages add index (siteid), add constraint FK657EFC4CA3B36A2 foreign key (siteid) references sites (id)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote:
Every run hibernate try to readd all the indexes (costraint) but the indexes are already there. Is this a bug or I'm missing something?


Thats normal. I don't know if there is a portable way to check if an index is allready there. Is this a problem? If yes, submit to JIRA.

Quote:
If the tables already exists hibernate will drop them even if I use only create and not create-drop.


create does not mean only create. The difference with create-drop is that Hibernate will then drop the tables again after the SessionFactory is closed.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 5:00 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 4:54 am
Posts: 25
Thank you for the confirmations!

Is there a configuration so that Hibernate will create the tables if they are not there and update them if they are outdated?

About the indexes update problem: yes, it is a problem because it add an index at every run and mysql has a 32 indexes limit for tables. I'll file the issue to JIRA!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 11:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote:
Is there a configuration so that Hibernate will create the tables if they are not there and update them if they are outdated?


Yes, thats exactly what update does

Quote:
About the indexes update problem: yes, it is a problem because it add an index at every run and mysql has a 32 indexes limit for tables.


Don't tell me that in MySQL creating the same index again won't replace the old one ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 11:33 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 4:54 am
Posts: 25
Quote:
Don't tell me that in MySQL creating the same index again won't replace the old one ...


I'm glad to tell you that ;-)

After three run I have the "site" index, the "site_2" index and the "site_3" index. Each of them is index for the site column.

In mysql you can have multiple identical indexes for the same column.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 11:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Could you probably try creating your own custom dialect extending MySQLDialect and implement the following Method in it:

Code:
public String getAddForeignKeyConstraintString(String constraintName, String[] foreignKey, String referencedTable, String[] primaryKey) {
   String cols = StringHelper.join(StringHelper.COMMA_SPACE, foreignKey);
   return new StringBuffer(30)
      .append(" add index ")
                .append(constraintName)
                .append(" (")
      .append(cols)
      .append("), add constraint ")
      .append(constraintName)
      .append(" foreign key (")
      .append(cols)
      .append(") references ")
      .append(referencedTable)
      .append(" (")
      .append( StringHelper.join(StringHelper.COMMA_SPACE, primaryKey) )
      .append(')')
      .toString();
}


Use this dialect in your hibernate.cfg.xml - I have no MySQL install to test it currently, so let me know if this works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:33 am 
Beginner
Beginner

Joined: Wed Feb 04, 2004 4:54 am
Posts: 25
it works!
thanks, I'll append this to the jira issue...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 8:31 pm 
Newbie

Joined: Mon May 10, 2004 3:00 am
Posts: 3
Hi Michael,

Is there any time where you would *not* want any of hibernate's dialects (not just MySQL) to only create one copy of each index/constraint?

Any chance of adding this one liner officially to CVS and closing the issue (HB-877)?

Thanks,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 9:35 pm 
Newbie

Joined: Sat Aug 09, 2008 9:33 pm
Posts: 1
I can confirm the error with the current versions of Hibernate and Hibernate Annotations while using MySQL.

Extending the dialect works (seems) flawlessly.

Why the issue has not been fixed?

Btw, I had to use "StringHelper.WHITESPACE" instead of "StringHelper.COMMA_SPACE"...hopes that it still ok.

Cheers!


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