2

I am trying to make my application run on MySQL (for production) and H2 (for dev/testing). My (Flyway) script to create the tables is almost identical now, except for a column that needs to be declared 'LONGTEXT' for MySQL. If I also use this for H2 (which is running in MySQL compatibility mode), I get:

Wrong column type in public.public.customer_license for column license.
Found: clob, expected: varchar(65535)

The Java code of my entity:

@Column(name = "license", length = 65535)
private String m_license;

If I change the column declaration to VARCHAR(65535), then it works for H2, but not for MySQL:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column length too big for column 'license' 
(max = 21845); use BLOB or TEXT instead

How can I make it work for both?

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • `BLOB` data type is also available with H2. You can use it in both H2 as well as MySQL. Here it is http://www.h2database.com/html/datatypes.html#blob_type – Hardik Bharadava May 20 '15 at 11:39
  • MySQL does not seem to like it. I get `Wrong column type in flux_licenses.customer_license for column license. Found: blob, expected: longtext` – Wim Deblauwe May 20 '15 at 11:41
  • i had supported 3 Databases in my Project, but all three has different classes and different Methods. i had used BLOB instead of LONGTEXT – Hardik Bharadava May 20 '15 at 11:44

2 Answers2

6

I had the same problem. I solved it by using the @Lob Annotation. This validates ok with LONGTEXT in a mysql table. When using an H2 in-memory, a CLOB field is created.

import javax.persistence.Lob;

...

@Lob
private String lotsOfText;
Simon Jenkins
  • 688
  • 8
  • 11
0

That is one of the reasons there is orm.xml, so you can have one mapping for one datastore, and one for a different datastore, and hence no need to recompile the code between runs

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
  • I am using Spring Boot, I don't have an `orm.xml`. Do you have a link to more documentation on how to do what you say here? – Wim Deblauwe May 20 '15 at 14:09
  • orm.xml is part of the JPA standard. You specify the location via the mapping-file part of persistence.xml. See also http://www.datanucleus.org/products/accessplatform_4_1/jpa/metadata_xml.html – Neil Stockton May 20 '15 at 14:12
  • @WimDeblauwe: See [this post](http://stackoverflow.com/questions/8750777/orm-xml-does-not-override-annotations) on SO as an example – olexd May 20 '15 at 14:28