hibernate column unique property not changing - java

Is there any way to change the column property from unique=true to unique=false without dropping the table?
Now I am stuck in the situation where tables has been created earlier and these table are containing data too. When I changed unique=true to unique=false it doesn't making any changes in table.

You can easily do this thing in the database. Suppose I have a table Person with Person_name having a unique constraint.
ALTER TABLE Person
DROP INDEX Person_name;
or
ALTER TABLE Persons
DROP CONSTRAINT Person_name;
If you try to achieve the same thing using hibernate, hibernate will try to drop and create the table again, which you do not want to happen.

Hmm... I don't know if you can allow yourself on database drop but you can try in your application properties file use:
spring.jpa.hibernate.ddl-auto=update
or
spring.jpa.hibernate.ddl-auto=create-drop
For more info:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
Also if you know that you will have a lot of changes in database schema and you want to make it easy to work you should get familiar with
https://www.liquibase.org/

Hibernate won't update to implement constraints on the table because there is a possibility of having an error as the existing table might have inconsistencies.

Related

Retrieve table hierarchie from Hibernate

Now and then I come into the situation that I have to display the table hierarchie of a database for further operations, currently in a data migration project where I have to treat "leaf tables" (tables which are leafes in the table dependency tree) in a different way.
I've always wanted to use Hibernate's meta information to retrieve and display the table dependency tree, but never knew how to approach the problem.
So can anyone give me feedback on whether Hibernate provides an API to do this? I am not asking for a complete solution, the information if there is an API and what it is called is absolutely sufficient.
I want to solve the following questions:
Which tables are in the database?
Is a given table a root table (not dependant from other tables)?
Is a given table a leaf table (dependant from other tables but no table is dependant from the given table)?
Which tables are dependant from the given table?
On which tables does the given table depend?
I know how to retrieve the mapping between entities and tables:
How to discover fully qualified table column from Hibernate MetadataSources , but I want the relationship between the tables.
In a custom MetadataContributor you can access metadataCollector.getDatabase() which exposes the full relational model to you. You just have so save that into a static volatile variable and then access it later on in your app to do whatever you want to do with it.

ADD/DELETE column from xml mapping

I have some problem with hibernate. when I add a column age into the file mapping of Client.xml, hibernate update my table client and add the column, but when a delete the same column age from Client.xml and client.java, and I run my application, I found that the column still in my table client.
can anyone have a clue why hibernate couldn't delete the column age from table client
thank you ^_^
Hibernate follow two approach
1. Schema first
2. Code first
In Schema first Java entities are created based on your database design and in Code first approach database is created with all java entities annotated with #Entity.
In your case you are following Code first approach, when you add any new column in java entity hibernate will create column on your behalf if hibernate.hbm2ddl.auto is update. if your database contains more tables hibernate has no issue with it that's why hibernate is not deleting any column if you delete it from Java Entity.

Set Storage Engine for Table in Hibernate

I am currently trying to set the Storage Engine for a table, because from case to case MyISAM and InnoDB should be used. Unfortunately I did not find a way to set this in Hibernate, and I do not want to create each table by hand. My prefered way would be to anotate it in the Java-POJO, but I couldn't find a way to do so.
I've found Hibernate: what's the difference between MySQLDialect and MySQLInnoDBDialect?, which tells me that setting the Dialect to org.hibernate.dialect.MySQLMyISAMDialect would help (it says this for the InnoDB-Dialect, but it seems like one could choose the default dialect with this), but this method has two shortcommings: First, the dialect is chosen for all tables, and second, it creates a query like CREATE TABLE xy(..) type=MyISAM but the query would be correct with engine=MyISAM.
Also Hibernate mysql innodb says that there are defaults, and that I can overwrite them when creating tables, but not how - does somebody know how to do this?
Instead of org.hibernate.dialect.MySQLMyISAMDialect or org.hibernate.dialect.MySQLInnoDBDialect, you should use org.hibernate.dialect.MySQL5MyISAMDialect or org.hibernate.dialect.MySQL5InnoDBDialect.
This way, it creates the table using engine=MyISAM or engine=InnoDB.
But this configuration is global and if you really have a reason to configure it for a single table, you could try:
Hibernate import SQL (hibernate.hbm2ddl.import_files). Add the path to a SQL file which contains the alter table into it.

Anyway to do Hibernate reverse engineering without putting foreign key in objects

Anyway to do Hibernate reverse engineering without putting foreign key in objects.
I am trying to do a Hibernate reverse engineering on my mysql database but I dont want the objects to show foreign keys.. Can this be done?
Exactly same problem here, you should use a new acces to this DB where alter table is not allowed.
So: create a new user with a DBA not add alter table permission and use this connection parameter to acces to this DB, this way alter table will not possible!
(workaround i know)
And the cause can be for this workaround e.g. Liferay doesn't use foreign keys and if hibernate mess up the table, the whole system fall apart...

Workaround for Spring/Hibernate due to non-standard behaviour of UNIQUE constraint in MS SQL

There is a UNIQUE database constraint on an index which doesn't allow more than one record having identical columns.
There is a piece of code, managed by Hibernate (v2.1.8), doing two DAO
getHibernateTemplate().save( theObject )
calls which results two records entered into the table mentioned above.
If this code is executed without transactions, it results INSERT, UPDATE, then another INSERT and another UPDATE SQL statements and works fine. Apparently, the sequence is to insert the record containing DB NULL first, and then update it with the proper data.
If this code is executed under Spring (v2.0.5) wrapped in a single Spring transaction, it results two INSERTS, followed by immediate exception due to UNIQUE constraint mentioned above.
This problem only manifests itself on MS SQL due to its incompatibility with ANSI SQL. It works fine on MySQL and Oracle. Unfortunately, our solution is cross-platform and must support all databases.
Having this stack of technologies, what would be your preferred workaround for given problem?
You could try flushing the hibernate session in between the two saves. This may force Hibernate to perform the first update before the second insert.
Also, when you say that hibernate is inserting NULL with the insert, do you mean every column is NULL, or just the ID column?
I have no experience in Hibernate, so I don't know if you are free to change the DB at your will or if Hibernate requires a specific DB structure you cannot change.
If you can make changes then you can use this workaround in MSSQL tu emulate the ANSI behaviour :
drop the unique index/constraint
define a calc field like this:
alter table MyTable Add MyCalcField as
case when MyUniqueField is NULL
then cast(Myprimarykey as MyUniqueFieldType)
else MyUniqueField end
add the unique constraint on this new field you created.
Naturally this applies if MyUniqueField is not the primary key! :)
You can find more details in this article at databasejournal.com

Categories

Resources