-->
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.  [ 8 posts ] 
Author Message
 Post subject: Can't create an index using Annotations
PostPosted: Fri Apr 27, 2007 2:12 pm 
Newbie

Joined: Fri Apr 27, 2007 1:34 pm
Posts: 3
Hi all. I'm new to hibernate and this seems like it should be simple but for the life of me I can't get a column indexed on my table. The two methods I've seen in docs are adding indexes in the Table annotation and just doing an Index annotation on a Column. I believe my sample below should do both of those. When I run this app, my table is created, but there are no indexes on STR1 or STR2:

mysql> describe TEST_INDEX;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| m_id | bigint(20) | NO | PRI | NULL | auto_increment |
| STR1 | varchar(255) | YES | | NULL | |
| STR2 | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Any help would be greatly appreciated.

------------------------------------------------------------------------


Code:
@Entity
@Table(name="TEST_INDEX")
@org.hibernate.annotations.Table(
    appliesTo = "TEST_INDEX",
    indexes =
      @org.hibernate.annotations.Index(
        name = "IDX_STR1",
        columnNames = { "STR1" }
      )
)
public class TestIndex implements Serializable {
    @Id @GeneratedValue
    @Column
    long m_id;
   
    @Column(name="STR1")
    String m_str1;
   
    @Column(name="STR2")
    @org.hibernate.annotations.Index(name = "IDX_STR2")
    String m_str2;
   
    /** Creates a new instance of TestIndex */
    public TestIndex() {
   m_str1 = "str1";
   m_str2 = "str2";
    }
   
    public static void main(String args[]) {
   Session session =
       HibernateUtil.getSessionFactory().openSession();
   Transaction tx = session.beginTransaction();
   TestIndex node = new TestIndex();
   session.save(node);
   tx.commit();
   session.close();
    }
}


Last edited by Matt Sivertson on Fri Apr 27, 2007 6:58 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 4:51 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Your entity is using field access type because your @Id annotation is on the field, not the getter -- all your getter annotations are then ignored. Either move all the annotations to the fields or the getters. If you absolutely must mix, you can use @AccessType to override.

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 6:16 pm 
Newbie

Joined: Fri Apr 27, 2007 1:34 pm
Posts: 3
Maybe I'm confused on the terminology, but I don't have any getter annotations in that case. I don't even have any getters, everything is on the fields. Am I missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 8:24 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Of course you don't. I should stay away from the forum when I'm tired!

What database are you using? What is your hibernate.hbm2ddl.auto setting?

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 8:47 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Works for me on Hibernate 3.2.2 with MySQL 5:

Code:
1211 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - drop table if exists TEST_INDEX
1248 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - create table TEST_INDEX (m_id bigint not null auto_increment, STR1 varchar(255), STR2 varchar(255), primary key (m_id))
1256 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - create index IDX_STR2 on TEST_INDEX (STR2)
1258 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport  - create index IDX_STR1 on TEST_INDEX (STR1)
1261 INFO  org.hibernate.tool.hbm2ddl.SchemaExport  - schema export complete


The above was done using hbm2ddl.audo=create. When I removed the index annotations and re-ran, it did not create indexes (obviously). I then added the index annotations back and re-ran the test with hbm2ddl.auto=update. The indexes were *not* created. I wonder if this is a bug or a feature...

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 11:53 am 
Newbie

Joined: Fri Apr 27, 2007 1:34 pm
Posts: 3
Thank you Chris! Changing my hbm2ddl.auto flag to create did the trick. It sure would be nice if it worked with update too, or maybe gave some kind of warning if the annotations don't match what's in the database.

Anyways, thanks again so much.

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 8:58 pm 
Newbie

Joined: Wed Apr 11, 2007 2:03 am
Posts: 5
Is it a bug ???


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 9:07 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Search JIRA. If it hasn't been reported, create a small test case, report the issue and attach the test case. See if it sticks :)

Contributing is fun.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


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