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

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")

Related

Quarkus data encryption with hibernate #ColumnTransformer: how to read key from application.properties?

I need encrypt/decrypt data with #ColumnTransformer (with Postgres DB) but I don't want an hardcoded key or store it in postgresql.conf.
I am able to get the key from application.properties with:
#ConfigProperty(name = "encryptionkey")
String key;
Unfortunately I cannot use it in #ColumnTransformer annotation because it requires a constant string.
Moreover I cannot use #AttributeConverter because I need do queries with "like" and I've tried yet but it did not work.
I have followed also this discussion which uses reflection for update annotation value at runtime : I would like to know if there is a way with Hibernate to perform a programmatic configuration of ColumnTransformer ? but it has not worked for me, infact I can replace the hardcoded key with "encryptionkey" when Quarkus starts but when I persists my entity, it has no effect.
Can anyone help me?
Thanks in advance.

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

Getting SQL script from Hibernate update

I'm looking for way to get the SQL update script when Hibernate automatically updates tables.
I'm using hibernate.hbm2ddl.auto=update in development environment only, and I need SQL script that updates tables for production.
I want these SQL scripts in txt format for revision and potential edit.
How can this be done?
Thanks for any advice.
There are some suggestions and general discussion here.
In a nutshell, you can turn on logging (to standard output):
hibernate.show_sql=true
Alternatively, if you use log4j, you can add this to your log4j.properties file:
log4j.logger.org.hibernate.SQL=DEBUG
Both of these approaches are going to output Hibernate's prepared statements with parameters (so the parameter values themselves are not inline). To get around this, you could use an interceptor like P6Spy. Details on that can be found here.
org.hibernate.cfg.Configuration class has method:
public java.lang.String[] generateSchemaUpdateScript( Dialect, DatabaseMetadata)
what generates the reqiured update script.
I've just implemented this in grails:
configuration = new DefaultGrailsDomainConfiguration(
grailsApplication: grailsApplication,
properties: props)
//this extends hibernate config
Connection c = SessionFactoryUtils.getDataSource(sessionFactory).getConnection(props.'hibernate.connection.username', props.'hibernate.connection.password')
<br/>md = new DatabaseMetadata(c, DialectFactory.buildDialect(props.'hibernate.dialect'))
configuration.generateSchemaUpdateScript(DialectFactory.buildDialect(props.'hibernate.dialect'), md)
)
check SchemaExport script in grails, for further information, it uses hibernate to generate schema.
(I had to implent is as a service because we have external domain model)

Generated queries contain schema and catalog name

I've the same problem as described here
In the generated SQL Informix expects catalog:schema.table but what's actually generated is
catalog.schema.table
which leads to a syntax error.
Setting:
hibernate.default_catalog=
hibernate.default_schema=
had no effect.
I even removed schema and catalog from the table annotation, this caused a different issues : the query looked like that ..table same for setting catalog and schema to an empty string.
Versions
seam 2.1.2
Hibernate Annotations 3.3.1.GA.CP01
Hibernate 3.2.4.sp1.cp08
Hibernate EntityManager 3.3.2.GAhibernate
Jboss 4.3 (similar to 4.2.3)
Note that there is a new (as of 2010-04-26) web page, http://www.iiug.org/opensource, that has information about using Informix software with various open source packages, including Hibernate. In particular, there is downloadable code that improves the interaction of Hibernate and Informix.
I ended up with the one of the worst hacks I evever did:
The colon is the offending char wich should be '.' catalog:schema.table.
Informix allows comments in SQL statements select {comment} * from sometable
So I set
hibernate.default_catalog={
hibernate.default_schema=}schemaname
the resulting code looks like
select * from {.}schemaname.tablename
which is accepted by informix query parser.
Delete the schema and catalog attributes from *.hbm.xml.

Categories