Longblob alternative in Hibernate + HSQLDB - java

I have a column in a MySQL database defined as LONGBLOB. This is the java code used on its mapping:
#Type(type = "org.hibernate.spatial.GeometryType")
#Column(columnDefinition = "LONGBLOB")
private Point latlng;
I want now to share this definition in an HSQLDB (for in-memory testing), but unfortunately HSQLDB doesn't have the LONGBLOB type.
I know about the HSQLDB alternatives such as LONGVARBINARY, but how I can instruct Hibernate to use LONGBLOB on MySQL and LONGVARBINARY on HSQLDB?
(I can't replace the #Column with #Lob; I tried that and Hibernate uses by default a "geometry" type instead of "longblob". Changing the current MySQL mapping is not an option.)

XML mappings with orm.xml overwrites annotated mappings.
So, you could include XML mapping file for this entity in /src/test/resources/ and define the proper datatype for this column.

Related

Alter table from jpa annotations in Java

I have a definition for a column like that:
#Column
private String my_column;
And by default in Postgres database type for this field is character varying(255).
Now, I want to change the data type for this column.
How I can do this without entry in database and alter table?
I tried this:
#Lob
#Column
private String my_column;
And
#Column(columnDefinition = "TEXT")
private String my_column;
But, without results.
The thing is, that JPA does not handle Schema changes.
JPA maps your existing DB to Java Classes, it does not manage the database it self.
As for schema changes managment.
A common practice is to have a schema migration tool to handle that, for example Flyway and Liquibase are a popular solutions.
There you can write a SQL script, to change the DB column type to "text"
and it will apply those changes when you run the DB migration process.
Or you can always just access your DB and modify it manually.

saving Oracle XMLType type column using JPA

I have a column type defined in Oracle as XMLTYPE and I am trying to save xml content using JPA. I have used something like in my entity class.
#CLOB
#Column(name = "COLOUM_NAME")
private String coloumName;
But i am getting the following exception.
ORA-01461: can bind a LONG value only for insert into a LONG column
Any solution for above issue?
You don't need #CLOB annotation here. Just use String type.

Where to set hibernate.id.new_generator_mappings property?

I've wasted too much time on this ...
I'm using oracle and I have a sequence (MY_TABLE_SEQ) defined which increments by 1.
In my Pojo I have:
#SequenceGenerator(name = "MY_SEQ", sequenceName="MY_TABLE_SEQ", allocationSize=50)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MY_SEQ")
This gives me a unique constraint issue. From my understanding I need to set the following property:
hibernate.id.new_generator_mappings=true
I've tried setting in my hibernate.cfg.xml file but it does not seem to make any difference. I've come across server post to place in persistance.xml but this is a standalone app, no webcontainer.
Setting allocationSize=1 works but of course it hits the db on each insert to get the next sequence. Setting the above property is suppose to resolve it.
I haven't tried Oracle, but I had similar issues to yours inserting into an AS400 DB2 table.
I had to remove the identity flag on the id column on DB2 table - and instead used a custom jpa/hibernate sequence generator. This is set up on the pojo/entity annotation of the #ID entity field as you've done.
DB2 had been giving me errors about missing SYSIBM.SYSSEQUENCES table, so evidently hibernate (version 5.2), doesn't recognize the native DB2 identity designation. A custom sequence was and effective workaround.
On the #ID entity field:
#GeneratedValue(generator = "table", strategy=GenerationType.TABLE)
#TableGenerator(name = "table", allocationSize = 20)
This example allocates a pool of 20 sequence numbers each time it queries the table.
Next, create the required table Hibernate needs with columns that match the hibernate5 API - must be in lower case ... so put quotes around the names to work around the auto-upper casing that DB2 defaults to. The API will error out if these names are in caps.
Table:
"hibernate_sequences"
example of 2 Columns used:
"sequence_next_hi_value" (integer, not nullable, 0 default)
"sequence_name" (character, sample length 20, not nullable, natural default)
In the configuration code for the dialect used - ex: Spring Boot programmatically, add these properties:
properties.put("hibernate.supportsSequences","false");
properties.put("hibernate.id.new_generator_mappings","false");
and in the *.properties file:
spring.jpa.properties.hibernate.dialect.supportsSequences=false
spring.jpa.properties.hibernate.id.new_generator_mappings=false
Database systems are case-sensitive for schema/table/field names. Also watch for typos everywhere, incl. property names.
Be sure your pojo/entity only contains private fields that will be mapped to the table. Static finals such as serialVersionUID are ok.
I will be doing someething similar for SQL Server soon.
For MySQL, I had no issues using an identity column as defined in a table ID field to insert records, so didn't have to make all these changes. A simpler setup since hibernate recognizes the identity designation in MySQL.
#GeneratedValue(strategy=GenerationType.IDENTITY)
was all that was needed in the pojo.
I'm a newbie at all this, so always looking for better ways ... but this worked for now.
I set the property like this in the hibernate.cfg.xml file and it works !
<property name="hibernate.jpa.compliance.global_id_generators" value="true"/>

Liquibase : change column type in Java

I am using liquibase:diff to update my database if one entity is modified.
How can I do in my Java entity if I want to transform VARCHAR(255) to TEXT in the MySQL column type.
In Java I only have a String type for my property.
Thank you.
If you're using JPA/Hibernate annotations, you can do this
#Column(columnDefinition = "TEXT")
but this would be database specific, and not portable across databases which don't have a "TEXT" column type.
A more portable solution would be to use the #lob annotation like
#Lob
private String text;
this should generate TEXT or LONGTEXT data type.

Is it possible to generate a default value for a certain database column using hbm2ddl

Env: JPA 1, Hibernate 3.3.x, MySQL 5.x
We auto generate database schema using hbm2ddl export operation. Would it be possible to generate a default value for a certain #Entity member during SQL generation. (e.g. archive field in mytable entity class.
create table mytable (
...
'archive‘ tinyint(1) default ’0 ’,
...
)
There is no portable way to do that and the columnDefinition "trick" is definitely not a good solution. Actually, setting defaults in the generated DDL is just not a good idea, this would require the provider to go back to the database to see the result after an insert1. Better default in your Java code.
1 Just in case, note that you can tell Hibernate to do that using the #Generated annotation.

Categories

Resources