Change schema dynamically - java

How can I set Postgres schema dynamically in Java? I tried doing:
this.getDataSource().getConnection().setSchema("mySchema");
I am using spring-jdbc and this is a JdbcDaoSupport instance.
Note: I don't want to go to database twice, so set search_path does not solve my problem efficiently.

Run the statement:
set schema 'myschema';
to change the current schema
Or simply set the search path, so that you can access the tables in e.g. the public and myschema:
set search_path to public, myschema;
(Note the difference in how you specify the schema name in the two statements: the first one has to use single quotes, the second one does not)
You can also change the search path permanently for a specific user, by using alter user....

Related

Spring boot Load properties from database

I have a requirement to load property code and description (an object will hold these values) from database. Currently I have these values being populated from properties file.
The values of these properties depend on the downstream system sending the response. I even have default values if I don't get a matching property from downstream.
What I have done so far:
Connect to Oracle db using hibernate to retrieve values from db.
Join two tables (one for default and another for system specific values) to get the data. Form list of objects from received data.
Compare data received from db with String variables I already have. Add property code and description if the variable name match with property name from db.
Though the code works as expected, this is not a clean solution. Can someone please suggest better approach for the same.

Need to create SQL statement dynamically in camel Talend

I am working with Talend ESB which uses camel.
My requirement is I need to do batch insert into a table, and have to create the query dynamically for different types of input.
If I create a context say extVar and default it to
"insert into table_foo (foo, bar) values (:#foo, :#bar)"
and then use cMessagingEndpoint component with following code
"sql"+context.extVar+"?batch=true&dataSource=mysql"
it works fine.
In Talend, I am able to access context.extVar inside cProcessor so I tried to load the query dynamically from there, but I couldn't.
Since I am using named parameters, I assume that I can neither use the body for query nor can I get the property/header value inside the sql statement of cMessageEndPoint.
Is there way where I can populate the query dynamically into sql statement of cMessageEndPoint.
I was able to solve my issue. Thanks to this answer. My objective was to have a dynamic batch insert query, with cRecipientList, I was able to achieve it. I was also able to get the context variables read and be used inside the Expression of cRecipientList while loading that dynamically from cProcessor.
Update:
I just found that you can also use .toD (available from camel version 2.16) to use simple expression language inside query.
For example: you could write,
.toD("sql:${property.query}?dataSource=ds_dwh_d&batch=true")

Using inet postgres datatype with OpenJPA

My application is using OpenJPA to connect with a Postgres database. In the schema I am using the inet postgres datatype in a column. This field in Java is a String. I am able to read the field correctly, but I am having problems inserting a new row.
Searching on the Internet I have found three possible solutions to do this:
Creating a Native query. This method works, but in my specific case creating a Native query in order to insert this row implies creating more queries that were being managed by OpenJPA which can lead to lots of bugs. So it is not the more suitable solution in this case.
Creating a PostgresDictionary like in this question: How to use Postgres inet data type with OpenJPA?. I have implemented this exactly how this user explains. I have created the custom PostgresDictionary, I have added the columnDefinition in the #Column annotation and I have added the property in the persistence.xml. But my custom PostgresDictionary is never called.
When the application created the PostgresDictionary keeps creating the org.apache.openjpa.jdbc.sql.PostgresDictionary instead of the custom one.
Implementing a custom Strategy, like this example http://webspherepersistence.blogspot.co.at/2009/04/custom-orm-with-openjpa.html. But in order to implement the Strategy, I have to set the type of the column from the class java.sql.Types (http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html?is-external=true) and there is no inet type in this column. I tried Types.OTHER, but I still have the same error indicating that the column is a type inet and the value I am trying to insert is varchar (String).
So, does anybody has an idea how to fix the problem I am having with the mapping?
The solution in the point 2 was not working because the openjpa.jdbc.DBDictionary was been overiden by the class org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter that had the database property set to POSTGRESQL. Which aparently set the DBDictionary to org.apache.openjpa.jdbc.sql.PostgresDictionary independently of the value set in the persistence.xml property.
Deleting this database property from the OpenJpaVendorAdapter allowed me to use my custom PostgresDictionary.

Liquibase - common columns?

In my db every table has 4 common columns - DATE_CREATED, USER_CREATED, DATE_MODIFIED, USER_MODIFIED, and I want to propagate this rule to all new tables implicitly.
Is it possible to do it without having to generate liquibase script manually?
This is not possible using liquibase (as far as I know).
The reason for this is simple:
What if you change your mind and add/remove one of the default columns later? If you want to change all tables then this is not possible with liquibase as this would mean changing all changesets, which is not allowed.
Use a DSL to generate your liquibase scripts then you can add a certain set of columns to every entity but an automatic way would be difficult with the way liquibase works.
There is nothing built into Liquibase to support this.
Your easiest option would be to use XML document entities which is purely XML-level and therefore transparent to Liquibase. They would allow you to attach common XML into your changelog files.
A more complex approach would be to use the Liquibase extension system (http://liquibase.org/extensions) which allows you to redefine the logic to convert changeSets into SQL. That would allow you to inject any logic you want, including common data types, standard columns, or anything else.
I do not think so.
My suggesion, Dont add above mentioned 4 columns in all tables because there are possible to keep null values in all table for existing entries.
please create a table like Primary key id, table or entity name and your four column name.

Check for column's character set and collation in JDBC

Is there a way to check if a certain MySQL column has a specific character set and collation with JDBC?
For those who need some background informations: The application I am working has changed its database layout with new versions. The update mechanisms was implemented rather basic: during startup, the application checks if the change is already there and, if not, alters the table accordingly. Right now I need to change an existing column to be unique and case sensitive (which means, I need to change the column's character-set and collation accordingly).
You will have to query it from INFORMATION_SCHEMA.COLUMNS. The CHARACTER_SET_NAME and COLLATION_NAME fields are what you need.
There is nothing in the JDBC spec that provides access to this.

Categories

Resources