I am using HSQL in memory Db for unit testing Java code. Its configured and brought up using Hibernate/JPA/Spring
By default its schema is PUBLIC. However the code I am testing has hardcoded schema. So I need to change the schema from PUBLIC to say ABC
I just figure out how to do it. I tried below approaches
CRATE SCHEMA ABC, SET INITIAL SCHEMA ABC
ALTER CATALOG RENAME PUBLIC TO ABC
I get the error 'Schema not found'
That led me to conclude that once setup and started, we cant change the schema. So I tried with configuring schema as a Entity Manager JPA property at env setup. I started getting 'Dependent Bean not found' error for one of the classes in "domain.packages.to.scan"
I searched a lot on the web, I couldnt find definitive answers on how to accomplish this ?
The simplest way is to rename the PUBLIC schema. Just use the correct syntax from http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_renaming
ALTER SCHEMA PUBLIC RENAME TO ABC
You can always rename the schema or change to a different schema that you create.
Related
I have a application where I used these technologies:
Grails 3.3.0
JDK 1.8
Spring 4+
Mysql 8
GORM 6.1.6.RELEASE
org.grails.plugins:spring-security-core:3.2.0
Hibernate 5+
Problem is when I am trying to connect the application with Mysql 8(with 5.6+ it is working fine), I am not able to get the information related to user from Grails-Spring-Security plugin.
The application is running even connect to DB but wont be able to authenticate or fetch the information of the User like findByUsername where username is property in my user domain class.
I have User domain class defined in application.properties file.
grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.aaa.User'
At some point I found this error but not sure whether it is related to this or not.
java.lang.IllegalStateException:
Either class [com.aaa.User] is not a domain class or GORM has not been initialized correctly or has already been shutdown.
Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
Want to understand why it wont be able to fetch the information from DB. I have tried lot of things like change the GORM version to 6.1.7 and grails spring-secuirty-core plugin version but not able to get anything.
Any help would be appreciated.
Thanks,
Atul
We are able to solve the issue. It happens when grails-hibernate plugin look for the property(in my case its username as I am calling API findByUsername) which is present in the static api or not. If it is not there it throws the exception.
The thing worked for me is, I have to put the property in static mapping of User domain class:
static mapping = {
table 'userTable'
id column: 'id'
password column: 'userpassword'
username column: 'username'
}
I am not sure why it is happening, when I run the application with Mysql5.6+ its working.(The driver is according to it) but when go with Mysql8, it look for the property in static mapping.
One more point I would like to mention to fix the issue is, make sure you have tablename, columnname same as defined in DB. Case sensitivity is what expected.
As I mentioned the case sensitivity, if you are using linux with Mysql8, the lower_case_table_names=0, this check with the following as per Mysql official documentation:
Table and database names are stored on disk using the lettercase
specified in the CREATE TABLE or CREATE DATABASE statement. Name
comparisons are case sensitive. You should not set this variable to 0
if you are running MySQL on a system that has case-insensitive file
names (such as Windows or macOS). If you force this variable to 0 with
--lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption
may result.
So if the table name is static is in camel or upper case and in db it is lower case it wont match. and the error occurred.
Hope this helps to someone.
I am trying to use hibernate framework with eclipse in order to build a simple web application and connect it with PostgreSQL. Here is my code:
Here is the error:
Does anyone know what I am doing wrong?
You should change property hibernate.hbmddl.auto value to create-drop or create to force hibernate create schema, because userclass table is not exists.
More information about hibernate.hbmddl.auto is here: https://stackoverflow.com/a/24417561/2055854
Just for those whom might encounter the same problem...
I just found the answer for it. Because of the database I am using is postgresql, I had to change the property "hibernate.dialect" in the persistence file. So, the result after the change was:
After that, the table was created!
Thank you.
I had a grails domain with a field named created Though now I've changed it to dateCreated. However, my database table still has the column named created so anytime I try to save a record grails complains saying Field 'created' doesn't have a default value even though I no longer have this field in my domain.
How does one get around this issue? Do I have to open my db and delete this column? In rails this is handled through migrations, what is the equivalent in grails?
If you just rename the domain field, you can specify column name in the mapping block, and not change it when you rename the related field. So, no DB changes will be needed at all.
class MyDomain {
Date dateCreated
static mapping = {
dateCreated column: 'created'
}
}
You must specified dbCreate = "update" in your DataSource.groovy.
If you are in development stage, you can change it to dbCreate = "create" to enable automatic schema refresh. Otherwise, in a production environment, you have to keep that configuration and alter the table manually.
You may refer to Grails DataSource doc, which also proposed some migration tools:
You can also remove the dbCreate setting completely, which is
recommended once your schema is relatively stable and definitely when
your application and database are deployed in production. Database
changes are then managed through proper migrations, either with SQL
scripts or a migration tool like Liquibase (the Database Migration
plugin uses Liquibase and is tightly integrated with Grails and GORM).
Coming from a mysql background, I am able to set the default schema name that I want to use for all my sql queries in the connection url. I now have an Oracle DB that I need to access. I am aware that I cannot specify the schema I want to use in the URL since the user is the schema name being used.
I realize that I can use a line of SQL code:
ALTER SESSION SET CURRENT_SCHEMA=default_schema
The project is using mybatis 2.3.5 as my SQL framework, but I am completely new to mybatis. Is there a simple way to configure mybatis to accomplish this? My application is a Spring 3 application, so I am using the Spring DataSourceTransactionManager to manage my transactions. I would presume that the manager must be made aware of this requirement to ensure that the command is sent whenever creating a new connection.
I've tried searching online, but most of the examples I find all have the schema names included within the sql queries in the SqlMaps, which I find to be bad practice.
In an ideal world, the schema name would be part of the URL such that I can make changes to the schema name for different environments (ex: dev, test, prod, etc) without touching the code (ie: only configured at the JNDI/application server level). I would be happy if I could use a Spring configuration value to set this as well as I could still use a JNDI lookup or a system environment property to retrieve the value.
Can anyone point me in the right direction?
Thanks,
Eric
As far as I know, there is no option in Oracle to change your URL in order to connect to a specific user schema.
1) mybatis: You may set your current schema to a deserved one before you start your operations. You can write your specification in a property file and set your method's arguments from that file. You do not need to change your code to change your schema in that case.
<update id="mySetSchemaMethod" parameterClass="String">
ALTER SESSION SET CURRENT_SCHEMA = ${schemaName}
</update>
2) trigger: If you are using this connection only for this particular java application, you can set a client event trigger so set your CURRENT_SCHEMA. This time, you need to change the trigger in order to manage test/prod changes.
CREATE OR REPLACE TRIGGER Set_Schema_On_Logon
AFTER LOGON
ON MY_SCHEMA
BEGIN
ALTER SESSION SET CURRENT_SCHEMA = MY_TEST_SCHEMA;
END;
I used to have a database called database and everything was working well using hibernate and its models.
I removed <property name="hibernate.hbm2ddl.auto"> to avoid update or create as it's a production server, we want to do it manually.
We recently switched to database2 and so we switched the hibernate configuration file and all the hibernate XML models.
`<class name="com.api.models.database.MmApplications" table="mm_applications" catalog="database2">`
but it keeps looking for database event if we migrated the database, the models and the connexion.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'database.mm_applications' doesn't exist
Does someone can help me ?
UPDATE ----
Hibernate is connecting to the right database (database2), but there is a prefix as a prefix database. making the queries hitting the database instead of database2, and when I try to force the default_schema my queries become :
`... from database.database2.mm_applications ....`
Any idea?
My database is specified in the hibernate.connection.url property. Have you changed that also ? An example would be: jdbc:mysql://localhost/mydatabase
Also, instead of removing hibernate.hbm2ddl.auto then perhaps you should set its value to validate. That way hibernate will ensure that the datamodel matches the database schema.
I found the problem, It was an other application deployed on the same tomcat server using hibernate as well with another database (database) making a conflict with the new application ...
There is still something weird, by connecting to any database, hibernate will use the specified catalog in the hibernate models and so constructing the request using the catalog.table_name
Hope this help someone someday.