【问题标题】:Hibernate disable alter table to add foreign key and disable alter table to add constraintHibernate禁用alter table添加外键并禁用alter table添加约束
【发布时间】:2022-01-25 16:47:13
【问题描述】:

这是我第一次使用 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&amp;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>

我正在寻找一种禁用自动创建外键的解决方案

请问我该如何解决这个问题? 提前谢谢你。

【问题讨论】:

    标签: java database hibernate


    【解决方案1】:

    真实世界的数据库迁移

    首先你不应该使用hibernate.hbm2ddl.auto=update。只有在使用 Hibernate 时才需要这样做。在实际项目中,我们使用 Liquibase 或 Flyway 进行数据库迁移。我们将hibernate.hbm2ddl.auto 设置为validatenone

    在数据库迁移中,您可以随意创建或不创建 FK。

    在 JPA 中命名 FK

    您还可以在 JPA 中为 FK 命名,以便 hbm2ddl 使用正确的名称创建它们。

    @ForeignKey(name = "fk_professor)
    @ManyToOne
    @JoinColumn(name = "professeur_code")
    private Professor professor;
    

    从 cmets 来看,这正是您真正想要的。但正确的方法仍然是使用数据库迁移工具。

    【讨论】:

    • 一开始我很挣扎,因为当我调用一个方法来更新表中的某些内容时,hibernate 会删除表并重新创建它我寻找解决方案并发现我必须设置 hibernate.hbm2ddl.auto=update 为FK 我必须尊重项目声明中给出的 FK 名称,所以我创建了两个,每个引用一个表,但是休眠重新创建另外两个外键
    • @Jasmine,我已经更新了答案以突出显示 FK 名称。
    • 先生,我正在努力从同一个项目的数据库中获取数据,你能帮我吗,这是问题:stackoverflow.com/questions/70500779/…
    【解决方案2】:

    谢谢@Stanilav Bashkyrtsev 我所做的如下:

    我设置hibernate.hbm2ddl.auto=none 并在数据库和代码中将 FK 名称更改为 ma​​tiere_referenceprofesseur_code 因为由于某种原因我忽略了休眠没有考虑我在数据库和 中添加的 FK @EmbeddableEnseignerId 包含外键,因此它一直在寻找 professeur_codematiere_reference 然后生成错误,因为它可以找到它们。

    但现在一切都很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 2012-02-10
      • 2015-11-14
      • 2018-12-16
      • 2013-06-27
      • 1970-01-01
      • 2018-04-27
      相关资源
      最近更新 更多