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.
Related
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.
I know that we can get the primary key of the newly inserted record with object.getId() when we do hibernate session.save(object). I want to understand how is hibernate getting this id. I want to achieve the same using a plain SQL query instead of using hibernate.
DB Server is MySQL.
I agree with #JB Nizet about generation: it depends on your annotation and, in any case, the id is assigned to your bean at the commit phase not before so the persistent provider must read the new generated id from database.
Many databases offer a mechanism for doing what is required. MySQL provides a function LAST_INSERT_ID(). More details here or there
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 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.
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.