這是我第一次使用 hibernate,每當我運行專案 hibernate 時,都會執行以下操作:
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
(資料庫中的欄位與java類中的欄位型別匹配)
這是資料庫
//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>
我正在尋找禁用自動創建外鍵的解決方案
我該如何解決這個問題?先感謝您。
uj5u.com熱心網友回復:
真實世界的資料庫遷移
你不應該hibernate.hbm2ddl.auto=update首先使用。只有在使用 Hibernate 時才需要這樣做。在實際專案中,我們使用 Liquibase 或 Flyway 進行資料庫遷移。我們設定hibernate.hbm2ddl.auto為validate或none。
在資料庫遷移中,您可以隨意創建或不創建 FK。
在 JPA 中命名 FK
您也可以在 JPA 中為 FK 命名,以便 hbm2ddl 使用正確的名稱創建它們。
@ForeignKey(name = "fk_professor)
@ManyToOne
@JoinColumn(name = "professeur_code")
private Professor professor;
從評論來看,這就是你真正要找的。但仍然正確的方法是使用資料庫遷移工具。
uj5u.com熱心網友回復:
謝謝@Stanilav Bashkyrtsev 我所做的如下:
我設定hibernate.hbm2ddl.auto=none
,改變了FK名matiere_reference和professeur_code資料庫和代碼,因為某種原因,我不理休眠沒有考慮FK我在資料庫中添加的@Embeddable類EnseignerId包含外鍵,所以它一直在尋找professeur_code和matiere_reference再生成錯誤,因為它可以找到它們。
但現在一切都好了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393625.html
下一篇:如何進行適當的物業管理
