Movies2actors未映射 - 带注释的Hibernate

时间:2016-02-21 09:32:27

标签: spring hibernate hql

我面临着使用JSF + Spring + Hibernate开发的一个令人困惑的问题。我尝试在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";

但是当它运行时我有一个错误:

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]

这是Hibernate生成的Movies2actors类:

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

}

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>

奇怪的是,我已经使用名为Ratings的表进行了HQL查询,该表与Movies2actors具有相同的结构,一切顺利。

任何人都知道这是什么问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的情况下,您应该在查询中使用完全限定名称,与您对Actors的名称相同,因此com.ml.model.Movies2actors