I encounter the following problem with Hibernate and foreign keys:
When I first deploy my web application, Hibernate was configured with this parameters (among many others):
databasePlatform set to "generic" (not engine specific) dialect org.hibernate.dialect.MySQLDialect.
hibernate.hbm2ddl.auto set to update
As the default engine was MyISAM, Hibernate logically created MyISAM tables with indexes, ignoring creation foreign keys (since MyISAM doesn't support such constraints).
Now that I want to migrate every tables to InnoDB, I would like Hibernate to automatically create missing foreign keys. Unfortunately, it looks like Hibernate is just looking for the index :
If the index exists, Hibernate will not create the corresponding foreign key;
If I drop the index, Hibernate will create both index and foreign key.
Since I don't want to drop every index in my schema, do you know a way to tell Hibernate to create the foreign key even if the index is created?
Thank you.
My suggestion would be to allow hibernate to recreate the entire schema since this is your development database.
If however, you have lots of test data you don't want to lose then I would create a script using the available MySQL commands to automatically drop all of your indexes for Hibernate to recreate them. The commands you will need are:
show tables;
show index from `table_name`;
drop index `index_name` on `table_name`;
Organise these into some form of program script (shell, python or the application your building etc.) and you're good to go.
Related
I have a MySQL database on which I want to generate entities with Dali JPA Tool built into Eclipse.
The database is not mine, and I prefer not to ask for the change of architecture.
The problem exists when I create entities because a table has a foreign key that is mapped to a column in another table that is not a primary key.
I have noticed that for this reason the entity of this table is not created by the tool (but it's added in persistence.xml).
Is there a way to generate the entity without having to change the database architecture?
Thanks a lot for every possible idea.
PS: I'm not a database expert, but is it ok to create foreign keys that are mapped to columns that are not primary keys?
I executed script related to create my database ..it executed successfully. but no foreign keys created . I am using cent os . mysql 5.0.
If the DB engine is one that does not support FK, it will show you no errors, but won't create them. For example, MyIsam.
Choose InnoDB as the table type to be able to create FK on it.
are the tables in MyISAM instead of InnoDB?
MyISAM don't support foreign keys.
I have recently switched jobs and at this new company we are using MySQL. I don't have any expereince with MySQL, although I have used SQL Server and Oracle for over 4 years now.
Now the strange thing I see with MySQL is that it does not seem to resepect some of the basic things like Foreign Key Constraints (meaning a column is a foregin key but i can insert any value here no matter if it's present in the other table where this FK related to). Now I know in SQL Server there is this concept of a NOCHECK foriegn key constraint but the guy at new company responsible for MySQL db say that not respecting a FK is a normal thing in MySQL and it does not need to have any special settings (like NOCHECK FK constraint).
I fail to understand that in a database system how can you ensure referential integirty without having these basic checks in place. I am not sure if the local mySQL "expert" know it well or it's just that mySQL really does not respect FK rules. Any thoughts?
Check that your tables are using the InnoDB engine. When using the MyISAM engine (which was the default until recently), foreign keys declarations are not enforced.
MySQL have different DB Engines -
MyISAM - default, no FK support
InnoDB - have FK support - but no fulltext search like in MyISAM
On both engines you can create table and try to create FK, but MyISAM will simply ignore it.
Also, make sure foreign keys are being enforced. For some reason they weren't on mine, leading to one week of headache!
Check:
SELECT ##FOREIGN_KEY_CHECKS
Set:
SET FOREIGN_KEY_CHECKS=1
I am using hibernate annotations with spring MVC.
Now i always create table using GUI editor like SQLYOG or phpmyadmin.
So i just create table with columns and even if i have some tables primaray key in other table , i just make that column with name like
Person ---id--name--age
SUbject------person_id----description
So i made those tables using GUI with mentioning anything about foreign key etc.
There is option in GUI editor ti make it primary kay and Auto increment so it usually works till now.
But i want to know that in Hibernate do i need to make tables with proper sql command . i mean do i need to mention which table is primary key and which is foreign key
or hibernate annotations are enough for that
Hibernate will work just fine as long as your annotations are correct.
But from a database design point of view I strongly suggest that you create at least the primary key and foreign key constraints.
Your queries will run much faster if you create the primary keys. You can improve performance a little more by creating other kinds of indexes to.
PHPMyAdmin is perfectly able to create primary keys for you, as well as every other index types there is in mysql. There's no need to write your creation code by hand.
I would like to make a view, and in that view alter the tables to have foreign keys.
From the MySQL manual can I see, that foreign keys only work on InnoDB, but my database is MyISAM.
So my question is, is it possible to create a view, and then create foreign keys in that view?
http:// dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
You have got everything completely wrong.
First of all MySQL allows you to use different table engines in a single table, so for example one table could be a MyISAM table and the other table could be an InnoDB table, it all depends on your need. The statement my database is MyISAM is completely wrong.
Secondly if you need for key constraints then use InnoDB tables and specify the constraints in the table definitions. You cannot specify foreign key constraints in views. Foreign key constraints are defined either when creating table or when altering the table.
A view is something else. Views are stored queries that when invoked produce a result set. See http://dev.mysql.com/doc/refman/5.0/en/views.html.