Can't map class to Hibernate without xml with ".setAnnotatedClasses()" Getting "2QuerySyntaxException: FileEntity is not mapped" error

Viewed 125

Hi everyone. I am trying to map class (FileEntity) to Hibernate using .setAnnotatedClasses() or .setPackagesToScan() methods. But I get the same error over and over again.


    org.hibernate.hql.internal.ast.QuerySyntaxException: FileEntity is not mapped

When I mapped the class with .xml all worked fine.

Please tell me, where is my mistake?

ApplicationContextConfig.java


    @Configuration
    @ComponentScan("com.group.appName")
    @EnableTransactionManagement
    @PropertySource("classpath:application.properties")
    public class ApplicationContextConfig {
    
        @Autowired
        Environment environment;
    
        @Bean(name = "viewResolver")
        public InternalResourceViewResolver getViewResolver() {
            return new InternalResourceViewResolver();
        }
    
        @Bean(name = "dataSource")
        public DataSource getDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setUrl(environment.getProperty("spring.datasource.url"));
            dataSource.setUsername(environment.getProperty("spring.datasource.username"));
            dataSource.setPassword(environment.getProperty("spring.datasource.password"));
            return dataSource;
        }
    
        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
            Properties properties = new Properties();
    
            properties.put("hibernate.dialect", environment.getProperty("spring.jpa.properties.hibernate.dialect"));
            properties.put("hibernate.show_sql", environment.getProperty("spring.jpa.show-sql"));
            properties.put("current_session_context_class",                 environment.getProperty("spring.jpa.properties.hibernate.current_session_context_class"));
    
            LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    
            factoryBean.setDataSource(dataSource);
            factoryBean.setHibernateProperties(properties);
            factoryBean.afterPropertiesSet();
            factoryBean.setPackagesToScan("com.group.appName.model");
    //      or another method
            factoryBean.setAnnotatedClasses(FileEntity.class);
            
            return factoryBean.getObject();
        }
    
        @Autowired
        @Bean(name = "transactionManager" )
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
            HibernateTransactionManager transactionManager
                = new HibernateTransactionManager(sessionFactory);
            return transactionManager;
        }
    
        @Bean(name = "multipartResolver")
        public CommonsMultipartResolver getCommonsMultipartResolver() {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
            multipartResolver.setMaxUploadSize(20971520); // 20MB
            multipartResolver.setMaxInMemorySize(1048576);  // 1MB
            return multipartResolver;
        }
    }

FileEntity.java


    @Entity
    @Table(name ="files_upload")
    public class FileEntity {
        private String fileName;
        private Byte[] fileData;
    
        @Id
        @Column(name = "file_name")
        public String getFileName() {
            return fileName;
        }
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
    
        @Column(name = "file_data")
        public Byte [] getFileData() {
            return fileData;
        }
        public void setFileData(Byte [] fileData) {
            this.fileData = fileData;
        }
    }

application.properties

server.port=9090

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/filedb?serverTimezone=Europe/Moscow
spring.datasource.username=root
spring.datasource.password=241299

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
1 Answers

Ok. I fixed it. The point is that in method getSessionFactory after creating an object of class LocalSessionFactoryBean the first line should be

factoryBean.setAnnotatedClasses(FileEntity.class);

So the correct creating an object of LocalSessionFactoryBean class is:

LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    
            factoryBean.setAnnotatedClasses(FileEntity.class); - first line.
            factoryBean.setDataSource(dataSource);
            factoryBean.setHibernateProperties(properties);
            factoryBean.afterPropertiesSet();
Related