-->
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.  [ 3 posts ] 
Author Message
 Post subject: JPA, schema export, and unlimited string column length
PostPosted: Wed Mar 24, 2010 10:52 am 
Newbie

Joined: Wed Mar 24, 2010 10:32 am
Posts: 7
Hi,

I'm using JPA 1 with Hibernate/JBoss5.1.0.GA as my provider. I have a few entities with String fields that need to be mapped to columns without length restrictions. I haven't seen any JPA documentation that describes a way to remove the character length restriction other than picking some arbitrarily large number. Since we always use PostgreSQL in our production and development environments we are working around this by using the @Column annotation like

Code:
  @Column(columnDefinition = "character varying")
  protected String content;


to remove the default 255 character limit. The problem we run into though is that our unit tests, using HSQL, fail because HSQL's unbounded character type is called 'longvarchar'. So my questions are:

1) Is there something in the JPA spec, or does Hibernate provide a vendor specific approach, that I am overlooking?
2) Assuming answer to (1) is "no, it's not possible", then is there something in the schema export that I can hook into and override column definition when running my JUnits? I'm thinking something along the lines of overriding a method or registering some callback that will receive the column info and alter 'character varying' to 'longvarchar'.

If this isn't possible then I'll have to resort to standing up a PostgreSQL db that automated tests run against, but I don't like this approach cause HSQL makes it really easy for developers and our build machine to run tests locally without having any external dependencies.


Top
 Profile  
 
 Post subject: Re: JPA, schema export, and unlimited string column length
PostPosted: Wed Mar 31, 2010 3:04 pm 
Newbie

Joined: Wed Mar 24, 2010 10:32 am
Posts: 7
*bump*

Surely I'm not the only one who's run into this problem


Top
 Profile  
 
 Post subject: Re: JPA, schema export, and unlimited string column length
PostPosted: Mon Nov 08, 2010 10:20 am 
Newbie

Joined: Mon Nov 08, 2010 9:20 am
Posts: 2
In case someone will need a solution for this.
1. Overwrite type mapping in dialects
Code:
class MyPostgreSQLDialect extends PostgreSQLDialect {
    public MyPostgreSQLDialect() {
        registerColumnType(Types.VARCHAR, Integer.MAX_VALUE, "character varying");
    }
}
class MyHsqlDialect extends HSQLDialect {
    public MyHsqlDialect() {
        registerColumnType(Types.VARCHAR, Integer.MAX_VALUE, "longvarchar");
    }
}

2. Specify corresponding length in mapping:
Code:
@Entity
class MyBean {
    ....
    @Column(length = Integer.MAX_VALUE)
    String unlimitedText;
}

3. In Hibernate configuration set property hibernate.dialect=my.package.MyPostgreSQLDialect
4. In test Hibernate configuration set property hibernate.dialect=my.package.MyHsqlDialect

After this Hibernate will use 'character varying' as column type for dev and production and 'longvarchar' for tests.


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