Valentin Garreau
Valentin Garreau

Reputation: 1063

Associative table not mapped - Hibernate + netbeans

My Tools :

enter image description here

HQL Query :

SELECT a.applicaitonName
FROM UserApp ua
    LEFT JOIN Application a On ua.applicationId= a.applicationId
WHERE
    ua.userId = 1

Error :

org.hibernate.hql.internal.ast.QuerySyntaxException: Userapp is not mapped [SELECT a.applicaitonName
FROM Userapp ua
    LEFT JOIN Application a On ua.applicationId= a.applicationId
WHERE
    ua.userId = 1]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)

How I proceeded to do that:

When I create Hibernate Mapping Files and Pojors from database, it created 2 news objects : Application and User. But not Userapp ...

do I have to create it manually ?

enter image description here

Here the hibernate.reveng.xml (Something strange, UserApp is write in blanck and not in grey like User and Application :

enter image description here

<hibernate-reverse-engineering>
  <schema-selection match-catalog="allin"/>
  <table-filter match-name="user"/>
  <table-filter match-name="application"/>
  <table-filter match-name="userapp"/>
</hibernate-reverse-engineering>

Thanks for your help !!

Upvotes: 1

Views: 509

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81988

Such mapping table normally don't get explicitly mapped by Hibernate but only implicitly by a ManyToMany mapping. Therefore your HQL statement should look somewhat like this

SELECT a.applicationName
FROM User u left join u.applications as a
WHERE u.userId = 1

I can't tell what the correct property name for applications is ... You'd should be able to look that up in the source code.

How to do this kind of joins in HQL is described here: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins

Upvotes: 0

Related Questions