Hibernate的ManyToMany简单使用
Hibernate's ManyToMany simple use
我有两个实体:CollabEntity
和 TechnoEntity
。
- 一个collab可以使用多个technos。
- 一个techno可以被多个collabs使用。
我想知道特定合作使用的所有技术。
我的数据库看起来像:
CREATE TABLE collabs(
co_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
co_firstname VARCHAR(25) NOT NULL,
co_lastname VARCHAR(30) NOT NULL,
);
CREATE TABLE technos(
te_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
te_tech VARCHAR(20)
);
CREATE TABLE cote( //Joins Collab & Techno
co_id INT NOT NULL,
te_id INT NOT NULL,
CONSTRAINT fk_cote_co_id FOREIGN KEY(co_id) REFERENCES collabs(co_id),
CONSTRAINT fk_cote_te_id FOREIGN KEY(te_id) REFERENCES technos(te_id),
PRIMARY KEY(co_id, te_id)
);
CollabEntity.java
@Entity
@Table(name = "collabs")
public class CollabEntity {
@Id
@GeneratedValue
@Column(name = "co_id")
private long id;
@Column(name = "co_firstname", nullable = false)
private String firstname;
@Column(name = "co_lastname", nullable = false)
private String lastname;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "technos",
joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
inverseJoinColumns = @JoinColumn(name = "te_id")
)
private Set<TechnoEntity> technos = new HashSet<TechnoEntity>();
protected CollabEntity() {
}
public CollabEntity(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public long getId() {
return id;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public Set<TechnoEntity> getTechnos() {
return technos;
}
}
TechnoEntity.java
@Entity
@Table(name = "technos")
public class TechnoEntity {
@Id
@GeneratedValue
@Column(name = "te_id")
private long id;
@Column(name = "te_tech")
private String tech;
@ManyToMany(mappedBy="technos")
private Set<CollabEntity> collabs = new HashSet<CollabEntity>();
protected TechnoEntity() {}
public TechnoEntity(String techno) {
this.tech = techno;
}
public long getId() {
return id;
}
public String getTech() {
return tech;
}
}
我试了好几个小时都没有找到解决方案。这是我的错误:
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'entityManagerFactory' defined in class path
resource [fr/xxx/dao/DatabaseConfig.class]: Invocation of init method
failed; nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory
准确地说:
Caused by: org.hibernate.MappingException: Foreign key
(FK_pc2r8qfxqv9jpgsfxrs61kfql:technos [te_id])) must have same number
of columns as the referenced primary key (technos [co_id,te_id])
在 CollabEntity
中更改多对多映射。您需要更改加入 table 名称。即名称从 technos 到其他名称。
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "collab_technos",
joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
inverseJoinColumns = @JoinColumn(name = "te_id")
)
我有两个实体:CollabEntity
和 TechnoEntity
。
- 一个collab可以使用多个technos。
- 一个techno可以被多个collabs使用。
我想知道特定合作使用的所有技术。
我的数据库看起来像:
CREATE TABLE collabs(
co_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
co_firstname VARCHAR(25) NOT NULL,
co_lastname VARCHAR(30) NOT NULL,
);
CREATE TABLE technos(
te_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
te_tech VARCHAR(20)
);
CREATE TABLE cote( //Joins Collab & Techno
co_id INT NOT NULL,
te_id INT NOT NULL,
CONSTRAINT fk_cote_co_id FOREIGN KEY(co_id) REFERENCES collabs(co_id),
CONSTRAINT fk_cote_te_id FOREIGN KEY(te_id) REFERENCES technos(te_id),
PRIMARY KEY(co_id, te_id)
);
CollabEntity.java
@Entity
@Table(name = "collabs")
public class CollabEntity {
@Id
@GeneratedValue
@Column(name = "co_id")
private long id;
@Column(name = "co_firstname", nullable = false)
private String firstname;
@Column(name = "co_lastname", nullable = false)
private String lastname;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "technos",
joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
inverseJoinColumns = @JoinColumn(name = "te_id")
)
private Set<TechnoEntity> technos = new HashSet<TechnoEntity>();
protected CollabEntity() {
}
public CollabEntity(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public long getId() {
return id;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public Set<TechnoEntity> getTechnos() {
return technos;
}
}
TechnoEntity.java
@Entity
@Table(name = "technos")
public class TechnoEntity {
@Id
@GeneratedValue
@Column(name = "te_id")
private long id;
@Column(name = "te_tech")
private String tech;
@ManyToMany(mappedBy="technos")
private Set<CollabEntity> collabs = new HashSet<CollabEntity>();
protected TechnoEntity() {}
public TechnoEntity(String techno) {
this.tech = techno;
}
public long getId() {
return id;
}
public String getTech() {
return tech;
}
}
我试了好几个小时都没有找到解决方案。这是我的错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/xxx/dao/DatabaseConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
准确地说:
Caused by: org.hibernate.MappingException: Foreign key (FK_pc2r8qfxqv9jpgsfxrs61kfql:technos [te_id])) must have same number of columns as the referenced primary key (technos [co_id,te_id])
在 CollabEntity
中更改多对多映射。您需要更改加入 table 名称。即名称从 technos 到其他名称。
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "collab_technos",
joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
inverseJoinColumns = @JoinColumn(name = "te_id")
)