休眠表未映射

时间:2020-02-13 15:13:22

标签: java hibernate hql

我有实体

Cache::tags(['people', 'artists'])->put('John', $john, $minutes);

Cache::tags(['people', 'authors'])->put('Anne', $anne, $minutes);

此实体的超类

@Entity
@Table(
        name = SomeEntity.TABLE_NAME,
        uniqueConstraints = {@UniqueConstraint(columnNames = {"first", "second"})}
)
public class SomeEntity extends SomeSuperEntity {
    public static final String TABLE_NAME = "SomeEntity";

    public SomeEntity() {
    }

    public SomeEntity(String first, String second, Integer third) {
        super(first, second, third);
    }
}

第二超类

@MappedSuperclass
public abstract class SomeSuperEntity extends SomeSuperAbstractEntity {
    @Column(nullable = false)
    private String first;
    @Column(nullable = false)
    private String second;

    protected SomeSuperEntity () {
    }

    protected SomeSuperEntity (String first, String second, Integer third) {
        super(third);
        this.first= first;
        this.second= second;
    }
}

我有这2种方法,第一种-用于查找实体

@MappedSuperclass
public abstract class SomeSuperAbstractEntity {
    @Column(nullable = false)
    private Integer third;

    protected SomeSuperAbstractEntity () {
    }

    protected SomeSuperAbstractEntity (Integer third) {
        this.third= third;
    }
}

第二个获取实体名称到hql

public <T extends SomeSuperEntity > Optional<T> findByFirstAndSecond(String first, String second, Class<T> tClass) {
        String hql = String.format("FROM %s E WHERE E.first = :first AND E.second = :second", getTable(tClass));
        return jpaApi.em().createQuery(hql, tClass)
                .setParameter("first", first)
                .setParameter("second", second)
                .getResultList().stream().findAny();
    }

当我尝试调用findByFirstAndSecond(“ 1”,“ 2”,SomeEntity.class)时,我遇到了错误

[错误] ciSomeMethod-错误:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射SomeEntity [FROM SomeEntity E WHERE E.first =:first AND E.second =:second] < / strong>

1 个答案:

答案 0 :(得分:1)

您是否已定义要在应用程序上下文中扫描的软件包?

<property name="packagesToScan">
<list>
    <value>com.package_to_someentity</value>
</list>
</property>

或者,在您hibernate.cfg.xml中定义它

<hibernate-configuration>
<session-factory>
   <mapping class="com.package_to_someentity.SomeEntity" />
</session-factory>
</hibernate-configuration>