Hibernate不会自动创建联接表

huangapple go评论99阅读模式
英文:

Hibernate doesn't creating Join Table on his own

问题

我想在我的实体之间建立多对多的关系。我希望Hibernate能自动生成这个表,但这似乎不会发生。

这是我的实体:

DrugsEntity

@Entity
@Table(name = "drugs", schema = "meds", catalog = "chinamed")
public class DrugsEntity {
    @Id
    @Basic
    @Column(name = "id")
    private int id;
    @Basic
    @Column(name = "name_eng")
    private String nameEng;

    @ManyToMany()
    @JoinTable(name = "drug_symptom",
            joinColumns = { @JoinColumn(name = "drug_id", nullable = false) },
            inverseJoinColumns = { @JoinColumn(name = "symptom_id", nullable = false) })
    private Set<SymptomEntity> symptoms;
}

SymptomsEntity

@Entity
@Table(name = "symptom", schema = "meds", catalog = "chinamed")
public class SymptomEntity {
    @Id
    @GeneratedValue
    int id;

    @Basic
    String name;

    @Column
    @ManyToMany(mappedBy = "symptoms", cascade = CascadeType.ALL)
    Set<DrugsEntity> drugs;
}

一些配置:

jpa:
  hibernate:
    ddl-auto: update
  show-sql: true

这样我可以看到Hibernate的SQL查询:

Hibernate: create table meds.diseases (disease_id int4 not null, drug_id int4, name varchar(255), primary key (disease_id))
Hibernate: create table meds.drugs (id int4 not null, action_pharm varchar(255), action_tkm varchar(255), clinic varchar(255), contraindications varchar(255), form varchar(255), maker varchar(255), name_eng varchar(255), name_ru varchar(255), taking_method varchar(255), primary key (id))
Hibernate: create table meds.herbal_drug (herbal_id int4 not null, drug_id int4, primary key (herbal_id))
Hibernate: create table meds.pattern_drug (pattern_id int4 not null, drug_id int4, primary key (pattern_id))
Hibernate: create table meds.symptom (id int4 not null, name varchar(255), primary key (id))
Hibernate: alter table if exists symptom_drug add constraint FKl7jx9kgpfiunb1r5neejqi1in foreign key (drug_id) references meds.symptom
Hibernate: alter table if exists symptom_drug add constraint FKimmnf4263cgg3yg46i90tkn1g foreign key (symptom_id) references meds.drugs
Hibernate: alter table if exists symptom_drugs add constraint FKnvs2tystmth11i573t29rtayk foreign key (drugs_id) references meds.drugs
Hibernate: alter table if exists symptom_drugs add constraint FKng4i6ig5nr2laew225ttfnrb9 foreign key (symptom_entity_id) references meds.symptom

正如我所看到的,Hibernate甚至没有尝试创建这个表。

使用的方言:org.hibernate.dialect.PostgreSQL10Dialect

如何使Hibernate能够自动创建这个表?

附注:已经查看了这个链接 https://stackoverflow.com/questions/52910621/hibernate-doesnt-create-join-table 但这不是我的情况,我想。

已解决:
问题在于这个表已经被创建。我可能可以在Hibernate的查询中注意到一些有趣的东西:在"drug_symptom"表名之前没有前缀"meds"。
所以它在数据库的"public"模式中创建了表。
我通过添加default_schema来解决这个问题。

spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        default_schema: meds

但我也可以在@JoinTable注释中使用"schema"参数。

英文:

I want to make many-to-many relationship between my entities.
I want hibernate to generate this table automaticaly, but it's not gonna happen.
Here is my entities:

DrugsEntity:

@Entity
@Table(name = &quot;drugs&quot;, schema = &quot;meds&quot;, catalog = &quot;chinamed&quot;)
public class DrugsEntity {
    @Id
    @Basic
    @Column(name = &quot;id&quot;)
    private int id;
    @Basic
    @Column(name = &quot;name_eng&quot;)
    private String nameEng;

    @ManyToMany()
    @JoinTable(name = &quot;drug_symptom&quot;,
            joinColumns = { @JoinColumn(name = &quot;drug_id&quot;,nullable = false) },
            inverseJoinColumns = { @JoinColumn(name = &quot;symptom_id&quot;, nullable = false) })
    private Set&lt;SymptomEntity&gt; symptoms;
}

SymptomsEntity:

@Entity
@Table(name = &quot;symptom&quot;,schema = &quot;meds&quot;, catalog = &quot;chinamed&quot;)
public class SymptomEntity {

    @Id
    @GeneratedValue
    int id;

    @Basic
    String name;

    @Column
    @ManyToMany(mappedBy = &quot;symptoms&quot;,cascade = CascadeType.ALL)
    Set&lt;DrugsEntity&gt; drugs;
}

Some configuration:

jpa:
hibernate:
  ddl-auto: update
show-sql: true

So i can see hibernate sql queries:

Hibernate: create table meds.diseases (disease_id int4 not null, drug_id int4, name varchar(255), primary key (disease_id))
Hibernate: create table meds.drugs (id int4 not null, action_pharm varchar(255), action_tkm varchar(255), clinic varchar(255), contraindications varchar(255), form varchar(255), maker varchar(255), name_eng varchar(255), name_ru varchar(255), taking_method varchar(255), primary key (id))
Hibernate: create table meds.herbal_drug (herbal_id int4 not null, drug_id int4, primary key (herbal_id))
Hibernate: create table meds.pattern_drug (pattern_id int4 not null, drug_id int4, primary key (pattern_id))
Hibernate: create table meds.symptom (id int4 not null, name varchar(255), primary key (id))
Hibernate: alter table if exists symptom_drug add constraint FKl7jx9kgpfiunb1r5neejqi1in foreign key (drug_id) references meds.symptom
Hibernate: alter table if exists symptom_drug add constraint FKimmnf4263cgg3yg46i90tkn1g foreign key (symptom_id) references meds.drugs
Hibernate: alter table if exists symptom_drugs add constraint FKnvs2tystmth11i573t29rtayk foreign key (drugs_id) references meds.drugs
Hibernate: alter table if exists symptom_drugs add constraint FKng4i6ig5nr2laew225ttfnrb9 foreign key (symptom_entity_id) references meds.symptom

As I see hibernate dont even try to create this table.

Using dialect: org.hibernate.dialect.PostgreSQL10Dialect

How can achive that hibernate create that table on his own?

P.S: already looked this link https://stackoverflow.com/questions/52910621/hibernate-doesnt-create-join-table But it isn't my case, i suppose.

SOLVED:
The problem was that this table was alredy crated. And I probably coud notice something interesting in hibernate's queries: there was no prefix "meds" before drug_symptom table name.
So it created table in "public" schema of databese.
Solved that this way: added default_schema

spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        default_schema: meds

But also I could use "schema" parameter in annotation @JoinTable.

答案1

得分: 2

从drugs字段中删除@Column注解,并在DrugsEntity.symptoms上定义级联类型,因为它是关系的拥有方。

@JoinTable和@ManyToMany注解都不应与@Column一起使用,因为映射必须通过其自身的定义进行。在同一字段上使用这两个注解会使Hibernate混淆。

您的映射应如下所示:

@Entity
@Table(name = "drugs", schema = "meds", catalog = "chinamed")
public class DrugsEntity {
    @Id
    @Column(name = "id")
    private int id;

    @Basic
    @Column(name = "name_eng")
    private String nameEng;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "drug_symptom",
        joinColumns = @JoinColumn(name = "drug_id", nullable = false),
        inverseJoinColumns = @JoinColumn(name = "symptom_id", nullable = false))
    private Set<SymptomEntity> symptoms;
}

@Entity
@Table(name = "symptom", schema = "meds", catalog = "chinamed")
public class SymptomEntity {

    @Id
    @GeneratedValue
    int id;

    @Basic
    String name;

    @ManyToMany(mappedBy = "symptoms")
    Set<DrugsEntity> drugs;
}

祝您编码顺利!

英文:

Remove @Column annotation from drugs field and define cascade type on DrugsEntity.symptoms, as it is the owning side of relationship.

Both @JoinTable and @ManyToMany annotations should not be used with @Column, as the mapping must be made by it's own definition. Hibernate gets confused about both annotations on same field.

Your mapping should be:

 @Entity
 @Table(name = &quot;drugs&quot;, schema = &quot;meds&quot;, catalog = &quot;chinamed&quot;)
 public class DrugsEntity {
      @Id
      @Column(name = &quot;id&quot;)
      private int id;

     @Basic
     @Column(name = &quot;name_eng&quot;)
     private String nameEng;

     @ManyToMany(cascade = CascadeType.ALL)
     @JoinTable(name = &quot;drug_symptom&quot;,
        joinColumns = @JoinColumn(name = &quot;drug_id&quot;,nullable = false),
        inverseJoinColumns = @JoinColumn(name = &quot;symptom_id&quot;, nullable = false))
     private Set&lt;SymptomEntity&gt; symptoms;
 }

and

@Entity
@Table(name = &quot;symptom&quot;,schema = &quot;meds&quot;, catalog = &quot;chinamed&quot;)
public class SymptomEntity {

    @Id
    @GeneratedValue
    int id;

    @Basic
    String name;

    @ManyToMany(mappedBy = &quot;symptoms&quot;)
    Set&lt;DrugsEntity&gt; drugs;
}

Good coding!

huangapple
  • 本文由 发表于 2020年4月6日 09:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61051727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定