hibernate not locating junction table in query of many to many relationship

CodeMed

In a spring mvc application using hibernate and jpa, I have a many to many relationship between a WordKey entity and a Concept entity. WordKey has a concepts collection, and Concept has a wordkeys collection. I DO NOT want to use fetchType=EAGER because performance is an issue. Instead, once a WordKey has been selected by a user, I want to populate a Collection<Concept> with the results of a query that takes selectedKeyWord wk as a parameter and returns the collection of concepts that is associated with that WordKey in the underlying database. How do I write this query in JPQL?

Here is the query that I have so far. It is not working (see error further below):

@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;
}

Here is the hibernate query that is being generated by the above code. Note that it is looking for an imaginary concept_wordkey table instead of using the wordkey_junction join table that is specified in the WordKey entity code further below:

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=?

The specific error being generated in the stack trace is:

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

Here is the Concept entity:

@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;
    }
}

Here is the WordKey entity:

@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;
    }
}
JamesENL

I think you need a mappedBy in your @ManyToMany(cascade={CascadeType.ALL}) in your Concept class. Add change your annotation to @ManyToMany(cascade={CascadeType.ALL}, mappedBy="concepts") and try it again.

You need this to tell Hibernate to map your many-to-many collection of wordKey's to the collection of Concept's on the WordKey class, which will in turn expose your wordkey_junction table to the collection.

If that doesn't work or you don't want a bidirectional relationship, let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

hibernate not locating junction table in query of many to many relationship

From Dev

Junction table/many to many relationship

From Dev

Hibernate query on many to many relationship with extra column

From Dev

Hibernate: many-to-many relationship table as entity

From Dev

Hibernate: many-to-many relationship table as entity

From Dev

NHibernate - Many to Many Query using Junction/Joiner Table

From Dev

One to many relationship with junction table using Entity Framework code first

From Dev

One to many relationship with junction table using Entity Framework code first

From Dev

Hibernate - Many to many relationship: relationship table not getting populated

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

query regarding one to many relationship in hibernate

From Dev

Do all relational database designs require a junction or associative table for many-to-many relationship?

From Dev

How to construct a Junction Table for Many-to-Many relationship without breaking Normal Form

From Dev

Creating many to many junction table in Entity Framework

From Dev

SQL join on junction table with many to many relation

From Dev

Creating many to many junction table in Entity Framework

From Dev

How to insert into associative table in many to many relationship by Hibernate?

From Dev

how to query many-to-many relationship in same table by redbeanphp?

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

Query for retrieving data from a bridging table of many to many relationship

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

Recursive relationship on a many to many table

From Dev

SQL many to many relationship table

From Dev

Many to many relationship in the same table?

From Dev

Hibernate one to many relationship with join table with addition columns in join table

From Dev

Hibernate query on many to many mapping

Related Related

  1. 1

    hibernate not locating junction table in query of many to many relationship

  2. 2

    Junction table/many to many relationship

  3. 3

    Hibernate query on many to many relationship with extra column

  4. 4

    Hibernate: many-to-many relationship table as entity

  5. 5

    Hibernate: many-to-many relationship table as entity

  6. 6

    NHibernate - Many to Many Query using Junction/Joiner Table

  7. 7

    One to many relationship with junction table using Entity Framework code first

  8. 8

    One to many relationship with junction table using Entity Framework code first

  9. 9

    Hibernate - Many to many relationship: relationship table not getting populated

  10. 10

    HQL equivalent query to criteria query in hibernate many to many relationship?

  11. 11

    HQL equivalent query to criteria query in hibernate many to many relationship?

  12. 12

    query regarding one to many relationship in hibernate

  13. 13

    Do all relational database designs require a junction or associative table for many-to-many relationship?

  14. 14

    How to construct a Junction Table for Many-to-Many relationship without breaking Normal Form

  15. 15

    Creating many to many junction table in Entity Framework

  16. 16

    SQL join on junction table with many to many relation

  17. 17

    Creating many to many junction table in Entity Framework

  18. 18

    How to insert into associative table in many to many relationship by Hibernate?

  19. 19

    how to query many-to-many relationship in same table by redbeanphp?

  20. 20

    How to query a pivot table data in MySQL (many to many relationship)

  21. 21

    Query for retrieving data from a bridging table of many to many relationship

  22. 22

    Query many to many relationship with DetachedCriteria

  23. 23

    how to query a many to many relationship?

  24. 24

    eloquent: query many to many relationship

  25. 25

    Recursive relationship on a many to many table

  26. 26

    SQL many to many relationship table

  27. 27

    Many to many relationship in the same table?

  28. 28

    Hibernate one to many relationship with join table with addition columns in join table

  29. 29

    Hibernate query on many to many mapping

HotTag

Archive