-->
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.  [ 4 posts ] 
Author Message
 Post subject: Annotation to generate char() instead of varchar()
PostPosted: Wed Feb 18, 2009 5:26 pm 
Newbie

Joined: Wed Feb 18, 2009 5:03 pm
Posts: 5
Hibernate version: 3.3.1.GA
Mapping documents: persistence.xml
Name and version of the database you are using: MS SQL 2005

I'm wondering if there is an annotation to have hbm2ddl generate a char() instead of a varchar() column type (without using columnDefinition.)

So, if I have a field like:
Code:
@Column(name="bork", length=4, nullable=false)   
private String bork;


Is there a way to get the column definition from hbm2ddl as
Code:
bork char(4) not null,

instead of
Code:
bork varchar(4) not null,


apart from using
Code:
@Column(name="bork", length=4, nullable=false,columnDefinition="char(4)")   


Thanks for any/all help. :).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 2:25 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
columnDefinition is for overriding the sql DDL fragment for a particular column. I dont know any other solution.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 6:23 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
If you want that Strings are always stored as char(), you have to subclass your dialect, e.g.:
Code:
import java.sql.Types;

import org.hibernate.dialect.MySQL5InnoDBDialect;


public class OwnDialect extends MySQL5InnoDBDialect {

    public OwnDialect() {
        super();
       
        registerColumnType( Types.VARCHAR, "char" );
        //the "255" marks the maximum length, for that this type should be used.
        registerColumnType( Types.VARCHAR, 255, "char($l)" );
    }
}

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 11:08 am 
Newbie

Joined: Wed Feb 18, 2009 5:03 pm
Posts: 5
Thanks guys, that's great. I didn't know about the length overload on registerColumnType, that's great.

Because most of the fields I wanted as char() are less than 5 characters long, I tried this:

Code:
// Default to type "char" for fields of 4 characters or less, default to "varchar" otherwise
registerColumnType( Types.VARCHAR, "varchar($l)");
registerColumnType( Types.VARCHAR, 4, "char($l)");


And I get "varchar()" for anything defined as 5 characters or more (unspecified length as varchar(255), char() otherwise. )


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.