英文:
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 = "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;
}
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 = "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;
}
and
@Entity
@Table(name = "symptom",schema = "meds", catalog = "chinamed")
public class SymptomEntity {
@Id
@GeneratedValue
int id;
@Basic
String name;
@ManyToMany(mappedBy = "symptoms")
Set<DrugsEntity> drugs;
}
Good coding!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论