gpt4 book ai didi

java - hibernate 在查询多对多关系时没有定位联结表

转载 作者:行者123 更新时间:2023-11-30 06:19:06 24 4
gpt4 key购买 nike

在使用 hibernate 和 jpa 的 spring mvc 应用程序中,我在 WordKey 之间建立了多对多关系。实体和一个Concept实体。 WordKey有一个 concepts收集,和Concept有一个 wordkeys Collection 。我不想使用 fetchType=EAGER因为性能是一个问题。相反,一旦一个 WordKey已被用户选择,我想填充 Collection<Concept>使用 selectedKeyWord 的查询结果wk作为参数并返回 concepts 的集合与WordKey相关联在底层数据库中。如何在 JPQL 中编写此查询?

这是我目前的查询。它不起作用(请参阅下面的错误):

@SuppressWarnings("unchecked")
public Collection<Concept> findConceptsForKeyWord(ConcWordKey wk) {
Query query = this.em.createQuery(
"SELECT DISTINCT concept FROM Concept concept join concept.wordkeys k WHERE k.name =:wk"
);
query.setParameter("wk", wk.getName());
Collection<Concept> result = (Collection<Concept>) query.getResultList();
return result;
}

这是由上述代码生成的 hibernate 查询。请注意,它正在寻找一个虚构的 concept_wordkey表而不是使用 wordkey_junction WordKey 中指定的连接表下面是实体代码:

select distinct conc0_.effectiveTime as effectiv1_52_,
conc0_.id as id2_52_,
from concept conc0_
inner join concept_wordkey wordkeys1_
on conc0_.effectiveTime=wordkeys1_.concept_effectiveTime
and conc0_.id=wordkeys1_.concept_id
inner join wordkey conc2_
on wordkeys1_.wordkeys_keyword=conc2_.keyword
where conc2_.keyword=?

堆栈跟踪中生成的具体错误是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:  
Table 'mydb.concept_wordkey' doesn't exist

这是 Concept实体:

@Entity
@Table(name = "concept")
public class Concept implements Serializable{

@EmbeddedId
public EmbedPK conceptPk;

@ManyToMany(cascade={CascadeType.ALL})
private Set<WordKey> wordkeys;

public EmbedPK getConceptPk(){return conceptPk;}

protected Set<WordKey> getWordkeysInternal() {
if (this.wordkeys == null) {this.wordkeys = new HashSet<WordKey>();}
return this.wordkeys;
}

public List<WordKey> getWordkeys() {
List<WordKey> sortedWordkeys = new ArrayList<WordKey>(getWordkeysInternal());
PropertyComparator.sort(sortedWordkeys, new MutableSortDefinition("wordkey", true, true));
return Collections.unmodifiableList(sortedWordkeys);
}

public WordKey getWordkey(String s) {return getWordkey(s, false);}

public WordKey getWordkey(String ps, boolean ignoreNew) {
ps = ps.toLowerCase();
for (WordKey s1 : getWordkeysInternal()) {
if (!ignoreNew || !s1.isNew()) {
String keyword = s1.getName();
keyword = keyword.toLowerCase();
if (keyword.equals(ps)) {return s1;}
}
}
return null;
}
}

这是 WordKey实体:

@Entity
@Table(name = "wordkey")
public class WordKey {

@Id
@Column(name="keyword")
private String name;

@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="wordkey_junction",
joinColumns={@JoinColumn(name="keyword")},
inverseJoinColumns={@JoinColumn(name="conceptid"),@JoinColumn(name="effectiveTime")})
private Set<Concept> concepts = new HashSet<Concept>();

public String getName(){return name;}
public void setName(String nm){name=nm;}

protected Set<Concept> getConceptsInternal() {
if (this.concepts == null) {this.concepts = new HashSet<Concept>();}
return this.concepts;
}

public List<Concept> getConcepts() {
List<Concept> sortedConcepts = new ArrayList<Concept>(getConceptsInternal());
PropertyComparator.sort(sortedConcepts, new MutableSortDefinition("conceptid", true, true));
return Collections.unmodifiableList(sortedConcepts);
}

public Concept getConcept(BigInteger s) {return getConcept(s, false);}

public Concept getConcept(BigInteger ps, boolean ignoreNew) {
for (Concept s1 : getConceptsInternal()) {
if (!ignoreNew || !s1.isNew()) {
BigInteger compName = s1.getConceptPk().getId();
if (compName == ps) {return s1;}
}
}
return null;
}
}

最佳答案

我认为您的 Concept 类中的 @ManyToMany(cascade={CascadeType.ALL}) 中需要一个 mappedBy。将注释添加到 @ManyToMany(cascade={CascadeType.ALL}, mappedBy="concepts") 并重试。

您需要这个来告诉 Hibernate 将您的多对多 wordKey 集合映射到 WordKey< 上的 Concept 集合 类,它将依次将您的 wordkey_junction 表公开给集合。

如果这不起作用或者您不想要双向关系,请告诉我。

关于java - hibernate 在查询多对多关系时没有定位联结表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23690281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com