Setting Hibernate 3 to use useJDBC4ColumnNameAndLabelSemantics - java

We are using Hibernate 3 and DB2 running into the:
Invalid parameter: Unknown column name FOO. ERRORCODE=-4460, SQLSTATE=null
problem which is a common problem I see when searching online. Basically, we need to force Hibernate to retrieve columns by column name. An example of a Stack Overflow question is here
If you read Mark Rotteveel's answer he suggests "set the useJDBC4ColumnNameAndLabelSemantics Connection or DataSource property to DB2BaseDataSource.NO (2)"
My question is, how do I this in Hibernate? I cannot find an example of how to set a JDBC property such as this. Does Hibernate have this property built in?

The manual clearly explains how to set JDBC connection properties.
If you use hibernate.properties to configure Hibernate, you add
hibernate.connection.useJDBC4ColumnNameAndLabelSemantics=2
to the file. If you use hibernate.cfg.xml, you add
<property name="connection.useJDBC4ColumnNameAndLabelSemantics">2</property>
to the session-factory element. You can also use org.hibernate.cfg.Configuration.setProperty() method.
Finally, you can simply append the property to the JDBC URL:
jdbc:db2://hostname:port/database:useJDBC4ColumnNameAndLabelSemantics=2;

Related

Hibernate columnDefault true needs to be mapped to sqlserver BIT default 1

I have a need to map the #org.hibernate.annotations.ColumnDefault("true") to BIT default 1 in generated SQL schema. Is there anyway other than adding a #Column(columnDefinintion="BIT default 1")
I'm using SQL Server 2008 Dialect and hibernate 5.3.26 and XPAND for code generation.
I have tried using #org.hibernate.type.BooleanType also but it did not work.
Done some debugging in hibernate core also and in org/hibernate/cfg/Ejb3Column.java:625 where it takes the #ColumnDefault annotation value and later set it as it is without mapping in org/hibernate/mapping/Table.java:561. May be I have missed something. Is there anyway to override this functionality?
Where is the SQL type and default value mapping happens in schema generation? classes methods etc.
Any help would be grateful.
Using the below combination resolved the issue.
#org.hibernate.annotations.Type(type = "org.hibernate.type.BooleanType")
#org.hibernate.annotations.ColumnDefault(value = "1")

How to connect to specific Schema in H2

So I have created a few schema in H2.
How can I connect to a specific schema in H2
For example when I need to connect to a specific schema in SQL Server I have below JDBC URL
jdbc:sqlserver://HOSTNAME:PORT;SelectMethod=cursor;instanceName=MYSCHEMA;databaseName=DBNAME
Is this feature available in H2.
If not is there a workaround.
I do not want to always access a particular table in my schema instance be accessed like MYSCHEMA.TABLE_NAME
Otherwise I suppose only way out will be to create all table into the default schema that is public
There is such feature supported. See this:
http://www.h2database.com/html/grammar.html#set_schema
You can specify the schema in the connection string:
jdbc:h2:test;SCHEMA=SCHEMA_NAME
You can also change the current schema with:
SET SCHEMA SCHEMA_NAME;
Hope this helps.
SET SCHEMA_SEARCH_PATH shemaName
http://h2database.com/html/grammar.html?highlight=drop%2Calias&search=drop%20alias#set_schema_search_path
You can also supply a schema property in the info parameter of
java.​sql.​DriverManager.getConnection(String url, Properties info).

How to tell Hibernate annotation #Column to be case-sensitive?

I'm trying to do a simple SELECT query in a table named ECM (in uppercase) on a Sybase db with Hibernate. I've annotated my DBO this way :
#Entity
#Table(name="ECM")
public class RelationshipDbo {
...
}
However, I'm facing a "table not found" error : the generated SQL has the table name in lowercase. I cannot change the database configuration to tell it to be case-insensitive.
I've also tried putting quotes like this :
#Table(name="`ECM`")
and this :
#Table(name="'ECM'")
Result : the quotes are added in the query, but the table name is still converted from uppercase to lowercase.
Technical information :
Hibernate 4.3
JPA 1.2
org.hibernate.dialect.SybaseDialect
Have you guys any idea?
EDIT: Also tried this Hibernate changes #Table(name) to lowercase
Then my columns names and table name are automatically quoted, but the names still get lowercased.
I think I have your answer:
Basically, you need to change the naming strategy for you JPA provider. How you do this will depend on how you setup your project.
In my case, using spring boot data I set a property in my application.properties to
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy
Without more details from you I can't give more specifics on how to do this.
My goal is a little different since was trying to create tables upper case and hibernate created them in lower case. Also i was using MySQL not Sybase.
But for me quoting the names like this worked:
#Entity
#Table(name="\"ECM\"")
public class RelationshipDbo {
...
}
Then tables were created upper case. Maybe that helps also for the queries.
What is your Sybase db version ?
SybaseDialect has been deprecated in Hibernate 3.5 and then refactored since Hibernate 4.1 with a bunch of subclasses matching different versions of Sybase. Have you tried one of the subclasses to see if it makes any difference?
org.hibernate.dialect.Sybase11Dialect
org.hibernate.dialect.SybaseAnywhereDialect
org.hibernate.dialect.SybaseASE15Dialect
Try this:
Use backticks as in #Table(name="`ECM`")?
This must work from Hibernate point. If not then problem should be in DB (if i'm not wrong)

HIBERNATE: Auto append word into TABLE

hi I have an entity with this annotation
#Entity
#Table(name = "REPORT_WORK")
But for some reason hibernate keep saying Missing Table: REPORT_REPORT_WORK
I know that is the problem because of when I change the name to "REPORT_WORKX"
It will say Missing Table: REPORT_REPORT_WORKX
Has any encountered this issue before?
Update: when I change the name to JJJJ
It will say Missing Table: REPORT_JJJJ
so for some reason there it is auto appending REPORT_
Configuration:
hibernate.hbm2ddl.auto=validate
I suspect that the problem is your Hibernate configurations. Specifically, if you don't have an appropriate setting for hibernate.hbm2ddl.auto, Hibernate won't automatically update the database schema when you change your model.
(And if you don't want the updates to happen automatically, then you need to figure out what schema changes are needed, code them as SQL DDL, and run them manually.)
Can you post your persistence.xml (or equivalent)?
It sounds like you are implementing org.hibernate.cfg.NamingStrategy, get rid of this configuration.
Some additional info:
JPA (Hibernate) and custom table prefixes

JPA and toplink create-table on if they don't already exist?

Looks like jpa is something which makes me ask a lot of questions.
Having added this
<property name="toplink.ddl-generation" value="create-tables"/>
my JPA application always creates tables when running, which results in exceptions in case the tables already exist. I would like JPA to check if the tables already exist and if not create them, however I could not find a value for the property above which does this.
So if I just turn it off, is there a way to tell JPA manually at some point to create all the tables?
Update here's the exception I get
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'tags' already exists
Error Code: 1050
Call: CREATE TABLE tags (ID BIGINT AUTO_INCREMENT NOT NULL, NAME VARCHAR(255), OCCURRENCE INTEGER, PRIMARY KEY (ID))
MySQLSyntaxErrorException?! Now that's wrong for sure
According to http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html#Java2DBSchemaGen toplink does not have an option to update exiting tables, I'm not sure if I would trust it to do the right thing anyway.
You could configure toplink to generate a sql script that you then would have to execute manually to create all tables. The filenames and location can be configured like this:
<property name="toplink.ddl-generation" value="create-tables"/>
<property name="toplink.ddl-generation.output-mode" value="sql-script"/>
<property name="toplink.create-ddl-jdbc-file-name" value="createDDL.sql"/>
<property name="toplink.drop-ddl-jdbc-file-name" value="dropDDL.sql"/>
<property name="toplink.application-location" value="/tmp"/>
I would like [my] JPA [provider] to check if the tables already exist and if not create them, however I could not find a value for the property above which does this.
Weird, according to the TopLink Essentials documentation about the toplink.ddl-generation extension, create-table should leave existing table unchanged:
TopLink JPA Extensions for Schema Generation
Specify what Data Descriptor Language
(DDL) generation action you want for
your JPA entities. To specify the DDL
generation target, see
toplink.ddl-generation.output-mode.
Valid values: oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
none - do not generate DDL; no
schema is generated.
create-tables - create DDL for
non-existent tables; leave existing
tables unchanged (see also
toplink.create-ddl-jdbc-file-name).
drop-and-create-tables - create DDL for all tables; drop all existing
tables (see also
toplink.create-ddl-jdbc-file-name
and
toplink.drop-ddl-jdbc-file-name).
If you are using persistence outside
the EJB container and would like to
create the DDL files without creating
tables, additionally define a Java
system property INTERACT_WITH_DB and
set its value to false.
Liquibase (http://www.liquibase.org) is good at this. It takes some time to get fully used to it, but I think it's worth the effort.
The Liquibase-way is independent of which JPA persistence provider you use. Actually, it's even database agnostic.

Categories