Search code examples
springhibernatehql

Movies2actors is not mapped - Hibernate with annotation


I am facing a confusing issue developping with JSF + Spring + Hibernate. I try to make a query in HQL :

private static String  HQL_GET_ACTORS      = "SELECT A FROM Actors A, Movies2actors M "
                                                       + "WHERE M.id.movieid = :movieid "
                                                       + "AND A.actorid = M.id.actorid";

But when it runs I have an error :

Movies2actors is not mapped [SELECT A FROM com.ml.model.Actors A, Movies2actors M WHERE M.id.movieid = :movieid AND A.actorid = M.id.actorid]

Here is the Movies2actors class generated by Hibernate :

package com.ml.model;
// Generated 13 f�vr. 2016 17:52:31 by Hibernate Tools 4.3.1.Final

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * Movies2actors generated by hbm2java
 */
@Entity
@Table( name = "movies2actors", catalog = "jmdb" )
public class Movies2actors implements java.io.Serializable {

    private Movies2actorsId id;

    public Movies2actors() {
    }

    public Movies2actors( Movies2actorsId id ) {
        this.id = id;
    }

    @EmbeddedId

    @AttributeOverrides( {
            @AttributeOverride( name = "movieid", column = @Column( name = "movieid", nullable = false ) ),
            @AttributeOverride( name = "actorid", column = @Column( name = "actorid", nullable = false ) ),
            @AttributeOverride( name = "asCharacter", column = @Column( name = "as_character", length = 1000 ) ) })
    public Movies2actorsId getId() {
        return this.id;
    }

    public void setId( Movies2actorsId id ) {
        this.id = id;
    }

}

And the application-context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/jmdb"/>
        <property name="username" value="john"/>
        <property name="password" value="is11seapol"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>com.ml.model.Movies</value>
                <value>com.ml.model.Actors</value>
                <value>com.ml.model.Directors</value>
                <value>com.ml.model.RatingsId</value>
                <value>com.ml.model.Users</value>
                <value>com.ml.model.Posters</value>
                <value>com.ml.model.Movies2actors</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <context:annotation-config />
    <context:component-scan base-package="com.ml.service"/>
</beans>

What is strange is that I already make a HQL query using a table named Ratings that has the same structure than Movies2actors and everything went well.

Anyone knows what is the problem ?

Thanks !


Solution

  • In your case, you should use the fully qualified name in your query, the same as you did for Actors, so com.ml.model.Movies2actors