我有两个实体:CollabEntity
和TechnoEntity
。
我想知道特定合作使用的所有技术。
我的数据库看起来像:
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;
}
}
我在没有找到解决方案的情况下尝试了几个小时。这是我的错误:
线程“main”中的异常 org.springframework.beans.factory.BeanCreationException:错误 在类路径中定义名为'entityManagerFactory'的bean resource [fr / xxx / dao / DatabaseConfig.class]:调用init方法 失败;嵌套异常是javax.persistence.PersistenceException: [PersistenceUnit:default]无法构建Hibernate SessionFactory
准确地说:
引起:org.hibernate.MappingException:外键 (FK_pc2r8qfxqv9jpgsfxrs61kfql:technos [te_id]))必须具有相同的编号 列作为引用的主键(technos [co_id,te_id])
答案 0 :(得分:3)
在public ActionResult Index(string id)
{
db.Configuration.LazyLoadingEnabled = false;
string searchString = id;
var songs = from m in db.Songs
select m;
if (!String.IsNullOrEmpty(searchString))
{
songs = songs.Where(s => s.Title.Contains(searchString));
}
var mappedSongs = songs.Select(it => new { Title = it.Title, Artist = it.Artist }).ToList();
return Json(mappedSongs , JsonRequestBehavior.AllowGet);
}
中更改多对多的映射。您需要更改连接表名称。即从technos命名为其他名称。
CollabEntity