AnnKont
AnnKont

Reputation: 506

Hibernate table not mapped

I've got entity

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

Super class of this entity

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

Second super class

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

    protected SomeSuperAbstractEntity () {
    }

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

And I have this 2 methods, first - for find entity

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

Second for get name of entity to hql

private String getTable(Class<? extends SomeSuperEntity > tClass) {
    if (tClass.equals(SomeEntity .class)) {
        return SomeEntity.TABLE_NAME;
    }
    return null;
}

When I try call findByFirstAndSecond("1", "2", SomeEntity.class), I have error

[error] c.i.SomeMethod- Error: org.hibernate.hql.internal.ast.QuerySyntaxException: SomeEntity is not mapped [FROM SomeEntity E WHERE E.first = :first AND E.second = :second]

Upvotes: 0

Views: 93

Answers (1)

steven35
steven35

Reputation: 4017

Have you defined the packages to scan in your application context?

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

Alternatively, define it in you hibernate.cfg.xml

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

Upvotes: 1

Related Questions