HQL JoinTable Not Available

I have many, many relationships between media and tags:

Medium:

@ManyToMany(fetch=FetchType.EAGER)
@IndexColumn(name="tags_index_column")
@JoinTable(name="tag_map",
          joinColumns={@JoinColumn(name="tag_id")},
          inverseJoinColumns={@JoinColumn(name="item_id")})
private List<Tag> tags;

Tags:

@ManyToMany(mappedBy="tags")
@JoinTable(name="tag_map",
        joinColumns={@JoinColumn(name="item_id")},
              inverseJoinColumns={@JoinColumn(name="tag_id")})
private List<Medium> media;

I try to query the connection table from hql but always get an exception:

String resultQueryString = "From tag_map"

        Query resultQuery SessionFactory.getCurrentSession().createQuery(resultQueryString);

An exception:

org.springframework.orm.hibernate3.HibernateQueryException: tag_map is not mapped [From tag_map]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: tag_map is not mapped [From tag_map]

What I basically want to do is request all media that have a tag with a specific identifier. Sorry, I cannot access the connection.

Any suggestions?:)

+3
source share
1 answer

HQL queries are written in terms of entities, not tables, so you cannot query an arbitrary table of tables.

So, you need to formulate the query in terms of the logical relationships between entites, and not directly access the connection table, something like this:

select m from Medium m join m.tags t where t.id = ?

See also:

+7

Source: https://habr.com/ru/post/1790922/


All Articles