Hibernate - function rand() in MYSQLDialect - java

I noticed that the MYSQLDialect defines rand() function as:
registerFunction("rand", new NoArgSQLFunction("rand", StandardBasicTypes.DOUBLE));
But, mysql allows use this function with argument (seed). [approve]
I have extended MySQLDialect and redefined this function and my code works correctly now. Nonetheless i want to know - this is bug or feature? Should i create an issue in hibernate bug tracker?
hibernate version: 4.3.6.Final

Yes this is bug.. I found the open issue in hibernate bug tracker.
https://hibernate.atlassian.net/browse/HHH-3777

Related

How to select schema at runtime using Hibernate?

I already have an existing code base, where schema(like db_1, db_2..) are created at run time.
We are currently using JdbcTemplate, using that its quite easy to append schema in the native SQL queries some thing like :-
sql = " Select * from "+schema+".user";
jdbcTemplate.query(sql, new UserMapper());
Now I want to know is how to provide schema to hibernate at runtime like I did with the jdbcTemplate?
What connection url should I provide in hibernate.cfg.xml so that it doesn't connects to a single schema rather whole database?
Any suggestions will be helpfull.
P.S: I am new to hibernate (So I might have missed something stupid)
I know of two options:
Use native SQL query binding results to JPA entities. Details here.
Use Hibernate multi-tenancy. Details here and here.
Although I haven't tried either.

How to migrate the UrlType from Hibernate 3.6 to 4.3 without changing database schema?

I'm in the process of migrating some code from Hibernate v3.4 to v4.3.10. As database I'm using H2.
When generating DDL, Hibernate 3.4 translates a java.net.URL field named myUrl into:
myUrl BINARY(255),
Now, when using Hibernate 4.3.10 the very same Java code gets "translated" into
myUrl VARCHAR(255),
Looking through documentation it seems that Hibernate 3.6 introduced a new type concept ("type registry") and specifically java.net.URL is not mapped to binary, but to varchar, which explains the different generated DDL above.
Question
How do I migrate my 3.4 code to 4.3.10 while not changing the database. I.e., I'd like to be able to read the old database with Hibernate 4.3.10 annotated code.
The code in question is currently trivial:
#Entity
// anything to add here?
public class SomeClass {
// what do I need to add here?
private java.net.URL myUrl;
...
}
To answer my own question - this seems to work:
#Entity
public class SomeClass {
#Lob
#Column(columnDefinition = "binary(255)")
private java.net.URL myURL;
...
}
Additional info: It's a bad idea to use properties of type URL as persistent properties, as during the url.equals(otherUrl) call the Java URLStreamHandler attempts to resolve the URL, i.e. a DNS lookup occurs. When you're on a slow connection, this will kill your performance, as you'll have to wait for the call to succeed or time out...

java.lang.NoSuchMethodError: javax.persistence.EntityManager.createQuery when using jpa / hibernate

We are running an app in jboss 5.1.0.GA, and it's complaining when we try to use the EntityManager.createQuery method. An implementation of this method is available in org.hibernate.ejb.AbstractEntityManagerImpl (and probably a few other places included in our code).
I suspect that it's picking up an older version of hibernate from the jboss libs directory.
How can I fix this?
Code looks something like this:
List<UserGroup> userGroups= DbHelper.getNonNullEntityManager().createQuery("SELECT ug FROM UserGroup ug", UserGroup.class).getResultList();
Exception is this:
java.lang.NoSuchMethodError: javax.persistence.EntityManager.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery;
Ok I fixed it by removing the Class parameter from the method. Seems that was added in a later method of hibernate:
List<UserGroup> userGroups= DbHelper.getNonNullEntityManager().createQuery("SELECT ug FROM UserGroup ug").getResultList();

Error when generating meta model code with JOOQ from SYBASE DB

I have this error when trying to generate the meta model with JOOQ:
org.jooq.exception.DataAccessException: SQL [select [SYS].[SYSUSER].[user_name] from [SYS].[SYSUSER]]; SYS.SYSUSER not found.
It's probably because the user does not have administration rights.
Can JOOQ generate metamodel code from create statements?
I don't have another user. Any suggestions?
You're probably using the wrong database as understood by jOOQ, as documented here:
http://www.jooq.org/manual/META/Configuration/
Note, how the documentation states:
<!-- The database dialect from jooq-meta. Available dialects are
named org.util.[database].[database]Database. Known values are:
org.jooq.util.ase.ASEDatabase
[...]
org.jooq.util.sybase.SybaseDatabase
You can also provide your own org.jooq.util.Database implementation
here, if your database is currently not supported -->
<name>org.jooq.util.oracle.OracleDatabase</name>
The SQL you've mentioned seems to be from the SybaseDatabase class (which corresponds to Sybase SQL Anywhere), whereas you should use the ASEDatabase class instead, generating calls to sp_help

Hibernate hibernate.hbm2ddl.auto=create/update/validate doesn't work for correctly for custom dialects?

I recently upgrade Hibernate from version 3.3.x to 3.6.4. In version 3.3.x validateSchema (hibernate.hbm2ddl.auto=validate) works correctly.
In version 3.6.x validation is broken (tested for 3.6.4 and 3.6.7 as well). The issue is relevant only for field type text.
I redefined the SQL type in my dialect e.g.
public class SQLServer2000UnicodeDialect extends SQLServerDialect {
public SQLServer2000UnicodeDialect(){
super();
// Use Unicode Characters
...
registerColumnType( Types.CLOB, "ntext" );
...
}
}
But during validation, hibernate use original SQL types instead of customized!
Wrong column type in db.dbo.table_name for column a_column. Found:
ntext, expected: text
It looks like a bug, but not sure if it is. Maybe I'm missing something in configuration?
P.S. hibernate.hbm2ddl.auto=create/update doesn't works correctly also!
P.P.S. My XML mapping configuration:
<property name="propName" type="text" column="a_column"/>
Did you look at this It looks like something similar. And the author claims it is working. And this.
As to your question, I suspect that while validation your class is somehow left out. Did you wire up your class properly and is JAR in the classpath etc?
This message :
Found: ntext, expected: text
tells me that you have a property of type ntext somewhere. What I believe you should be doing is : to continue using hibernate types in your schema, and let hibernate handle converting the types into the database specific type using the dialect. Do not use custom types in XML mapping files.
It's a bit strange, but hibernate somewhere in versions 3.4-3.6 changed binding of "text" mapping (actually hibernate have two types of mapping for long strings: clob and text).
In version 3.3 both mapping (clob and text) were mapped to Types.CLOB.
In version 3.6 you have to provide separate mapping for text and for clob. Fixing my dialect as shown below solved my issue:
public class SQLServer2000UnicodeDialect extends SQLServerDialect {
public SQLServer2000UnicodeDialect(){
super();
...
registerColumnType( Types.CLOB, "ntext" );
registerColumnType( Types.LONGVARCHAR, "ntext" );
...
}
}

Categories

Resources