This is my first time using hibernate and whenever I run the project hibernate does this:
Hibernate: alter table Enseigner add column matiere_reference varchar(255) not null
Hibernate: alter table Enseigner add column professeur_code integer not null
Hibernate: alter table Enseigner add constraint FKbuufx1q63fjcj0h9aaix4cbu3 foreign key (matiere_reference) references matiere (reference)
Hibernate: alter table Enseigner add constraint FKk5idavh0u5pwc1n41h11y7tn3 foreign key (professeur_code) references professeur (code)
Hibernate: select professeur0_.code as code1_2_, professeur0_.login as login2_2_, professeur0_.nom as nom3_2_, professeur0_.pass as pass4_2_, professeur0_.prenom as prenom5_2_ from professeur professeur0_ order by professeur0_.code
(fields in database match the fields type in java classes)
this is the database
//manyToMany
hibernate.cfg.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/schoolDB?useSSL=false&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">eschool</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">1</property>
<mapping resource="com/Entity/Professeur.hbm.xml"/>
<mapping resource="com/Entity/Matiere.hbm.xml"/>
<mapping resource="com/Entity/Enseigner.hbm.xml"/>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
I'm looking for a solution to disable creating foreign keys automatically
how can I solve this issue please? thank you in advance.
CodePudding user response:
Real-world DB Migrations
You shouldn't use hibernate.hbm2ddl.auto=update
in the first place. This is needed only to play with Hibernate. In real projects we use Liquibase or Flyway for DB migrations. And we set hibernate.hbm2ddl.auto
to validate
or to none
.
And in the DB migrations you can create or not create FKs as you please.
Naming FKs in JPA
Also you can give names to FKs in JPA so that it hbm2ddl creates them with the right names.
@ForeignKey(name = "fk_professor)
@ManyToOne
@JoinColumn(name = "professeur_code")
private Professor professor;
Judging from the comments this is what you were actually looking for. But still the proper way is to use DB Migration tools.
CodePudding user response:
Thank you @Stanilav Bashkyrtsev What I did is the following :
I set hibernate.hbm2ddl.auto=none
And changed the FK names to matiere_reference and professeur_code in database and code because for some reason I ignore hibernate didn't consider the FK I added in database and in the @Embeddable class EnseignerId
that contains foreign keys, so it kept looking for professeur_code
and matiere_reference
then generate an error because it could find them.
But now it's all good