0

I'm getting exception Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Building is not mapped [from Building] My Building class mapped

@javax.persistence.Entity
@Table(name = "building")
public class Building extends AbstractModel {

AbstractModel is empty (just for upcast)

Setting packagesToScan

    @Primary
    @Bean
    @Autowired
    public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
       ....
        localContainerEntityManagerFactoryBean.setPackagesToScan("com.app.persistence.model");
      ....
    }

Code throws excetion

public <M extends AbstractModel> List<M> findAll() {
    List<Building> buildings;

    try {
        buildings = (List<Building>) getHibernateTemplate().find("from Building");
    } catch (Exception e) {
        throw e;
    }
    return (List<M>) buildings;
}

Also i setuped

    @Bean
    public LocalSessionFactoryBean localSessionFactoryBean(DataSource ds) throws ClassNotFoundException {
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource());
        return localSessionFactoryBean;
    }
MolecularMan
  • 227
  • 2
  • 16
  • Looks like something wrong with `Building` class itself or with `AbstractModel`. Hard to say, a lot of details are omitted – Andremoniy Feb 13 '17 at 11:04
  • Why are you configuring a JPA EntityManagerFactory but start using plain Hibernate?! You also need to setup the `SessionFactory` accordingly (although I would suggest to simply use JPA instead of plain hibernate). – M. Deinum Feb 13 '17 at 11:04
  • are you using javax.persistence.Table or org.hibernate.annotations.Table? – Kalaiarasan Manimaran Feb 13 '17 at 11:06
  • javax.persistence. annotations, also other entities in project have same problem – MolecularMan Feb 13 '17 at 11:09
  • i have to use hibernate 5 in this project – MolecularMan Feb 13 '17 at 11:09
  • If you need Hibernate 5, configure a Hibernate `SessionFactory` bean instead of JPA `EntityManagerFactory`. Also please give the lines above `buildings = (List) getHibernateTemplate().find("from Building");` – Ramanujan R Feb 13 '17 at 11:21
  • i setuped LocalSessionFactoryBean – MolecularMan Feb 13 '17 at 11:22
  • The fact that you use hibernate doesn't mean you cannot use JPA... Hibernate is a JPA provider... So the argument that you have to use Hibernate5 doesn't really hold (as you can perfectly use that and still use it with JPA). – M. Deinum Feb 13 '17 at 11:23
  • Don't add additional code as comments edit your question instead. – M. Deinum Feb 13 '17 at 11:24

1 Answers1

1

You are configuring an EntityManagerFactory which is for use with JPA however in your code you are using the plain Hibernate API, which requires a correctly configured SessionFactory instead.

Instead of using plain hibernate I strongly suggest to simply use JPA instead. Just rewrite your code to use an EntityManager instead of Session and/or HibernateTemplate (The latter is something you should avoid using as that isn't recommended anymore since hibernate 3.0.1!).

@PersistenceContext
private EntityManager em;

public <M extends AbstractModel> List<M> findAll() {
    return em.createQuery("select b FROM Building b", Building.class).getResultList();
}

And remove the setup of plain hibernate i.e. the LocalSessionFactoryBean configuration and HibernateTemplate setup.

This is all you need. Now if you would add Spring Data JPA into the mix you don't even need this, you would only need a BuildingRepository interface.

public interface BuildingRepository extends JpaRepository<Building, Long> {}

Assuming that the id is of type Long.

If you really want to use plain Hibernate (which as stated is something I wouldn't recommend) you need to configure your LocalSessionFactoryBean correctly and specify the packagesToScan for it as well.

@Bean
public LocalSessionFactoryBean localSessionFactoryBean(DataSource ds) throws ClassNotFoundException {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(dataSource());
    localSessionFactoryBean.setPackagesToScan("com.app.persistence.model");
    return localSessionFactoryBean;
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Unfortunately hibernate 5 is a part of technical specifications of this project, so i can't avoid it – MolecularMan Feb 13 '17 at 11:32
  • 1
    As stated before the fact that you use hibernate, doesn't mean that you cannot use JPA... Hibernate == JPA Provider.. So you can adhere to that technical spec perfectly while using JPA. – M. Deinum Feb 13 '17 at 11:35
  • thanks for advice, i will operate your answers if anything and use Spring Data – MolecularMan Feb 13 '17 at 11:42