Hibernate 禁用 alter table 添加外键并禁用 alter table 添加约束
Hibernate disable alter table to add foreign key and disable alter table to add constraint
这是我第一次使用 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 类 中的字段类型)
这是数据库
//多对多
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>
我正在寻找禁用自动创建外键的解决方案
请问我该如何解决这个问题?
提前谢谢你。
真实世界的数据库迁移
首先你不应该使用 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;
从评论来看,这正是您真正想要的。但正确的方法还是使用数据库迁移工具。
谢谢 @Stanilav Bashkyrtsev
我所做的是以下内容:
我设置hibernate.hbm2ddl.auto=none
并在数据库和代码中将 FK 名称更改为 matiere_reference 和 professeur_code 因为出于某种原因我忽略了休眠没有'不考虑我在数据库中添加的 FK 和包含外键的 @Embeddable class EnseignerId
,所以它一直在寻找 professeur_code
和 matiere_reference
然后生成错误,因为它可以找到它们。
但现在一切都很好
这是我第一次使用 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 类 中的字段类型)
这是数据库
//多对多
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>
我正在寻找禁用自动创建外键的解决方案
请问我该如何解决这个问题? 提前谢谢你。
真实世界的数据库迁移
首先你不应该使用 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;
从评论来看,这正是您真正想要的。但正确的方法还是使用数据库迁移工具。
谢谢 @Stanilav Bashkyrtsev 我所做的是以下内容:
我设置hibernate.hbm2ddl.auto=none
并在数据库和代码中将 FK 名称更改为 matiere_reference 和 professeur_code 因为出于某种原因我忽略了休眠没有'不考虑我在数据库中添加的 FK 和包含外键的 @Embeddable class EnseignerId
,所以它一直在寻找 professeur_code
和 matiere_reference
然后生成错误,因为它可以找到它们。
但现在一切都很好