Hibernate/JPA - do operations on table only if exists - java

I am looking for an answer if it is possible or not in hibernate.
What I am trying to achieve is that if a particular table exists in the DB then the application should do all the regular operations with it (which exists in the code - find,save.. etc.).
Else just ignore the table (#Repository) and the fields in the #Entity class, and will skip all the related code.
I have the same question regarding ignoring particular non-existing field of an existing table and the field is annotated with #Column, is it possible to ignore the field if it does not exist in table?
I want to use save method of JPA but which can ignore that field if needed.

That's impossible because with Hibernate you map a Class to table and the table and all the mapped columns MUST exist.
What you are trying to do is dynamic SQL and there you would need to read the database dictonary to check what exsits and generate the code during runtime.

Related

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.

How it impacts hibernate if we change foreign key name from database?

I'm working on a legacy application which uses Hibernate and MySQL. In one of my DB tables, I've found duplicate foreign key constraints. Names are like the following:
FK3EBE45E8C4027E24
FK3EBE45E8F5ADD75E
Now I want to drop one index and rename another one from database only. Will there be any impact on hibernate functionalities?
No
There will not be any impact on the Hibernate code. Only when you make changes to the structure of the table - add/remove/rename a column, change the datatype, then there will be an impact as you will have to make changes to the DTO. MySQL Indexes are abstractions for Hibernate. Hibernate doesn't care whether there's an index or not. It will create a query and send to the database.
Renaming a constraint will be impact only on automatic schema update (create). Hibernate will try to delete constraint by name and generate an exception. It is not a problem (for Hibernate 5, don't know about other versions), a schema update will not stop.
If you don't use automatic schema update, you will not have any problems.

Hibernate: Update table generator

Is there a way to tell Hibernate to first check if the current primary key generated by a Table Generator is usable or outdated?
I have an application which uses hibernate to create new entries in several tables in my database, but sometimes these generated values are outdated and already used. This happens because this database is used by quite a few applications and scripts, and some of these use the "select MAX(ID)+1"-Keygeneration"strategy". It is not really an option to change all other components to use the table generator (although it would solve the problem), so I have to make sure that the values I get from the table generator are really usable.
Is there any way to tell Hibernate to check the validity of the generated values before it tries to insert a new record into the database (and throw a ConstraintViolationException)?
Or, alternatively, is there a way to manually update the generator tables before hibernate uses them to generate new Ids?
The obvious way would be to run a native query like UPDATE pk_generator SET value=(SELECT MAX(ID)+1 from members) WHERE column='members'
When you save a object with saveOrUpdate() the objects id field will get updated with the auto generated id if it was a create operation. So that it will never conflict with id which was already generated and used.

Hibernate - Retrieve all table info - column name, indexes, length and populate as table

Do we have any hibernate function which can be used to retrieve all the table info such as column name, indexes, length and populate as table in the front end.
If not possible in hibernate, do we need to write DB dependent SQL script for the same.
This will help to provide a page for diagnosis when required.
Thanks.
You can use JDBC DatabaseMetaData.
But if you want a state-of-the-art database schema Java representation, then there's only one tool to satisfy your need, and its name is jOOQ.
You can even generate jOOQ table mappings from JPA Annotations.
You can also use the DSLContext.meta() or even the jooq-meta module, if you just want the code generation part of jOOQ.

java hibernate eclipse xml - use an entity without a primary key

I have one question that if we are creating any table in database i.e. not having any primary key(all columns values are nullable).
can't we do mapping without ID field in Hibernate O/R Mapping i.e. .hbm.xml file while working with hibernate.
The problem which i am facing is my .hbm.xml file without ID field is not getting validated.
I got the answers from u all, but i have one question that can we use transient keyword prefixed to ID variable in the entity class so that not to persist that value into database?
The only real solution is to fix your broken data model. An entity that does not have a primary key is not relational data, so you can't expect an ORM product to be able to know how to handle it.
And how Hibernate will perform update without a primary key or an uniquely identified row?
Thanks.
You cannot do that in hibernate. And you shouldn't do it anyway. Databases warn you that it is not advisable not to have a primary key.
If you really don't have a set of columns that form a unique key, you have two options:
add an auto-increment primary key (preferred)
use raw JDBC to get the data from that particular table. You can do that with hibernate's session.doWork(..) method.

Categories