Home Hibernate not persisting foreign keys in Mysql
Reply: 0

Hibernate not persisting foreign keys in Mysql

user1383
1#
user1383 Published in April 25, 2018, 6:12 pm

I have an entity Property which has city field. Something like this:

@Entity
class Property {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    private City city;

    ...
}

So each property (e.g house or apartment) belongs to only one city.

Now here is a thing, if I try to log generated SQL by Hibernate, it is generating the foreign key in a right way:

Hibernate:

 alter table property 
    add constraint FKdn1hnohufcwdr4a401xabcjn 
    foreign key (city_id_city) 
    references city (id_city)

However, if I check my Mysql database, there is no foreign key there, only index is generated:

show create table property;

leads to:

| property | CREATE TABLE `property` (
  `id_property` int(11) NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `deal_type` varchar(15) NOT NULL,
  `publisher_type` varchar(15) NOT NULL,
  `type` varchar(15) NOT NULL,
  `updated_at` datetime NOT NULL,
  `city_id_city` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_property`),
  KEY `FKdn1hnohufcwdr4a401xabcjn` (`city_id_city`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |

So the question is why there is no CONSTRAINT FOREIGN KEY () REFERENCES definition in my database?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.350401 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO