How to turn off the foreign constraint by using the hibernate? - java

I am importing and exporting between two databases, and the problem when I want to add some tabels to the new it raises the error regarding the constrains.
I know how to turn this directly by using the mySQL:
SET FOREIGN_KEY_CHECKS=0
however, I want to use something like sission or query to send this command to the database.
I have tried something like this:
Query q = session2.createQuery("SET FOREIGN_KEY_CHECKS=0");
and this also didn't work.

Related

How to don't write schema name in oracle sql query?

I have Workspace/Schema EDUCATION in Oracle XE.
In my Java code I want execute queries like this: SELECT * FROM Table instead of SELECT * FROM EDUCATION.Table.
When I write query without EDUCATION I have error: table or view does not exist.
I tried to set the default schema to % (screenshot), but it did not help.
How to avoid writing Workspace/Schema name?
If I understand correctly, you want to access tables in other schemas without using the schema name.
One simple way to do this uses synonyms. In the schema you are connect to:
create synonym table for education.table;
Then you can use table where you would use education.table.

Hibernate save SQL exception

Hi guys I am trying to save an object to a MySQL data base via Hibernate. if I execute following code
User user = new User();
user.setData_1("my data 5");
user.setFirstname("Freddy");
user.setLastname("Bob");
user.setId(5);
session.save(user);
session.getTransaction().commit();
I get a
'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'xxx.my_table_1' doesn't exist'
exception. However, querying from the same table using the same config works just fine.
What could be the issue?
Check your connection string in the configuration something like hibernate.connection.url = jdbc:postgresql://localhost/mydatabase You may be missing the schema name in the url(mydatabase).
So, after some trial and error, I came to find out that the issue with the .get() (and apparently .save() too) was that I did not have a hibernate.default_schema set in config. Looks like it is used to create the 'dynamic' SQL for .save() and .get(), but if you use .createSQLQuery(), it just uses what ever String you pass as an argument for the SQL, and therefor works with out needing to have hibernate.default_schema set.

Change Table names in derby database using entitymanager

I am using an APACHE DERBY database, and basing my database interactions on EntityManager, and I don't want to use JDBC class to build a query to change my tables' names (i just need to put a prefix to each new user to the application, but have the same structure of tables), such as:
//em stands for EntityManager object
Query tableNamesQuery= em.createNamedQuery("RENAME TABLE SCHEMA.EMP_ACT TO EMPLOYEE_ACT");
em.executeUpdate();
// ... rest of the function's work
// The command works from the database command prompt but i don't know how to use it in a program
//Or as i know you can't change system tables data, but here's the code
Query tableNamesQuery= em.createNamedQuery("UPDATE SYS.SYSTABLES SET TABLENAME='NEW_TABLE_NAME' WHERE TABLETYPE='T'");
em.executeUpdate();
// ... rest of the function's work
My questions are :
This syntax is correct?
Will it work?
Is there any other alternative?
Should I just use the SYS.SYSTABLES and find all the tables that has 'T' as tabletype and alter their name their, will it change the access name ?
I think you're looking for the RENAME TABLE statement: http://db.apache.org/derby/docs/10.10/ref/rrefsqljrenametablestatement.html
Don't just issue update statements against the system catalogs, you will corrupt your database.

Querying the appropriate database schema

This is a follow-on question to my earlier question about specifying multiple schemata in java using jooq to interact with H2.
My test H2 DB currently has 2 schemata, PUBLIC and INFORMATION_SCHEMA. PUBLIC is specified as the default schema by H2. When running a query that should extract information from eg INFORMATION_SCHEMA.TABLES the query fails with a "table unknown" SQL error. I am only able to execute such queries by executing a factory.use(INFORMATION_SCHEMA). There are no build errors etc and eclipse properly autocompletes eg TABLES.TABLE_NAME.
If I dont do this, jooq doesnt seem to prepend the appropriate schema even though I create the correct Factory object for the schema eg
InformationSchemaFactory info = new InformationSchemaFactory(conn);
I read about mapping but am a bit confused as to which schema I would use as the input/output.
By default, the InformationSchemaFactory assumes that the supplied connection is actually connected to the INFORMATION_SCHEMA. That's why schema names are not rendered in SQL. Example:
// This query...
new InformationSchemaFactory(conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL (with the asterisk expanded):
SELECT * FROM "TABLES";
The above behaviour should be documented in your generated InformationSchemaFactory Javadoc. In order to prepend "TABLES" with "INFORMATION_SCHEMA", you have several options.
Use a regular factory instead, which is not tied to any schema:
// This query...
new Factory(H2, conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL:
SELECT * FROM "INFORMATION_SCHEMA"."TABLES";
Use another schema's factory, such as the generated PublicFactory:
// This query...
new PublicFactory(conn).selectFrom(INFORMATION_SCHEMA.TABLES).fetch();
// ... renders this SQL:
SELECT * FROM "INFORMATION_SCHEMA"."TABLES";
Use Settings and an appropriate schema mapping to force the schema name to be rendered.
The first option is probably the easiest one.
This blog post here will give you some insight about how to log executed queries to your preferred logger output: http://blog.jooq.org/2011/10/20/debug-logging-sql-with-jooq/

How to force treat MySQL Constraint Errors from Hibernate

I have a Java Web Project that uses Hibernate and MySQL. I have trouble to treat exceptions like when i try to insert a new register at database with the same primary key (Intentionally)
i got the following error with a message box "Could not insert:...". But i don't want to shows directly to user this error, i want to treat when i call "Persisten.save()", but in my code it doesn't appears nothing wrong (for my ide, i threat all possible excepetions).
So how can i change (configuration/code) to force threat exceptions like that and change the message?!
You should check if the Id already exists in the database before you try to persist it.

Categories