Hibernate Schema Validation Fails on Oracle Table Synonyms [duplicate] - java

This question already has answers here:
JPA Entiy on synonym instead of table
(3 answers)
Closed 4 years ago.
I'm developing a Java web application that uses Hibernate (annotations-based) for persisting entities to an Oracle 11g database. The DBA created synonyms for the tables and requested that I use these synonyms instead of the physical tables. (Eg: Table "Foo" has synonym "S_Foo")
If I have "hibernate.hbm2ddl.auto=validate" enabled, then the application fails on startup with "Missing Table: S_Foo". If I turn off the validation, then the app starts up fine and works properly. My guess is that Hibernate only checks against physical tables and not synonyms when validating that a table exists.
Is there any way to enable Hibernate schema validation with synonyms? Can I specify both a physical table and a synonym in the annotation? I prefer having that extra safety check that the table structure is correct when the application starts up.

I'm not to familiar with hibernate, but could you try views instead of synonyms. If you are just using these tables for views, it would work the same as a synonym. If you want to be able to do CRUD on the "table" though you'd need to build a bunch of instead-of triggers.

Change hibernate.hbm2ddl.auto=validate to hibernate.hbm2ddl.auto value = "" then it won't fail.

Related

How Hibernate saves entity to database

Will Hibernate automatically write the state of an object into a byte-stream i.e. serialization and save it to database?
I want to know how Hibernate saves a entity to database. Please correct me if I have anything wrong.
Hibernate use jdbc and sql.
As user of hibernate i suggested to understand SQL (especially subjects: select,join`s, index, query planning (how to see and interpret query plan)(last is more advanced))
You can see actual sql for some profile when develop your application using for example https://p6spy.readthedocs.io/en/latest/configandusage.html

JPA Hibernate query vs Native query

I use Spring Data JPA (hibernate) generated queries for fetching data from my Sqlserver. Now i am getting performance related issues in my system.
Load findByLoadId(Integer loadId);
This is the query i am using to get data. This query returns 25 cell data but i only use 5 data from that.
can i use direct native query like
select id,date,createdBy,createdOn,loadName from Load where
loadId=:loadId
but if native query is suggestable then I am having question like Does ORM frameWork reduce performence by getting unneeded data from Database?
By "data cell" I assume that you are referring to database table columns, and not to records. The answer to your question is that yes, ORM frameworks might tend to just do a SELECT * under the hood, which can result in unwanted information being sent across the network to your application. If the JPA repository interface is behaving this way, you may switch to either an explicit JPA query (e.g. using the #Query annotation), or even a native query. Then, just select the columns you want. The issue here is that ORM frameworks map object templates (e.g. classes) to entire database tables. So, the concept of entity implicitly includes every database column. If you go with the option of selecting only certain columns, you may need to do some juggling on the Java side. Note that if the use a JPA query, your code would still, in theory, be database independent.

HIbernate doesn't create table that already exist in another database

I have a bean entity named com.test.Student which is mapped to a table named "student" in attendance database using xml. The "student" table in attendance database does not yet exist.
Hibernate is not creating the "student" table on attendance database on application start, even though the property hibernate.hbm2dll.auto is set to "update". It is also not generating any exception, warning or query. It simply does not do anything.
After some testing, I came to realize that a student table already exists in some other database named "sms". If I map the com.test.Student #Entity to another table name (that does not exist in any database), Hibernate will create it just fine with this config.
Why is Hibernate not creating the "student" table on the mapped database? Is the existance of another table with the same name in another database interfering in some way?
Some info:
Hibernate ver: 4.3.9
Spring ver: 4.3.9
MySQL as DB
Java 8
Netbeans asIDE
As mentioned on the comments section, if hibernate is not creating tables on ddl auto due to table name conflicts between schemas, you should explicitly declare:
#Table(name="attendance.student")
That way hibernate will create the table correcly. If no schema is explicitly declared there, it will look for other tables with the same name publicly, and therefore, wont create the new one.
Take a look at this bug description. If you want to know more details about how hibernate behaves when creating new tables, you should definately create another post with a more concise scenario, so that we can discuss it further.

Is there a way to let hibernate auto manage database?

Hibernate automatically performs some updates such as creating tables or columns, but don't changing types of columns. For example we are changed column type from long to int and column type in database still bigint (PostgreSQL 9.5). Also, we added type converter for LocalDateTime fields, and Hibernate creating new field as timestamps but don't changing type of old fields. How can we configure Hibernate to let it automatically manage such things?
While I think this practice is pretty bad, and very dangerous, the reality is you just need the right permissions.
Most SQL databases store the database information in a system schema. The user for your app would have to have permission to utilize and possibly CRUD that schema. Once you have that, it is just a matter of writing the hibernate classes to manage the tables.
For example, if I wanted to change the schema a particular table belonged to I can do that by executing this statement in PostgreSQL:
update pg_tables set schemaname = 'newSchema' where tablename = 'xxx';
Allowing your application to do so opens you up to all kinds of pain and suffering. Including faults that are expecting a certain data model that was dynamically updated; and, if your application is hacked you could have all your tables dropped.

Is it possible to check if an Informix table has a primary key [duplicate]

This question already has answers here:
Query to check if primary key exists on the table in informix
(2 answers)
Closed 7 years ago.
Is it possible to check (with a query) if a primary key of an Informix table exists? I have to check this from Java code via the EntityManager from javax.persistence.
Chris311, See the link below where similar question is asked and solved:
Query to check if primary key exists on the table in informix
The user first looks for index name for the PK (pk_idx column)
then, checks the index columns (look for the same index name of the PK constraint).
Any database support special commands that retrieve information about the database schema itself and/or allow to change it.
For example MySQL supports command SHOW CREATE TABLE (see here).
Informix' version of similar command is called info. Please take a look on the following discussion for details: Informix SQL - List all fields & tables
Java persistence API can run any command that is supported by current database including management commands, so you can run info command too. I'd recommend you to start from playing with this command using any UI or command line tool that runs commands against your database. Once your command works try to run it from java.

Categories