如果没有xml,则无法将类Map到hibernate,“.setannotatedclasses()”获取“2querysyntaxexception:fileentity未Map”错误

zkure5ic  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(257)

大家好。我正在尝试使用.setAnnotatedClass()或.setpackagestoscan()方法将类(fileentity)Map到hibernate。但我一次又一次地犯同样的错误。

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

当我用.xmlMap类时,一切都很好。
请告诉我,我的错在哪里?
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;
        }
    }

文件实体.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;
        }
    }

应用程序属性

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
nzk0hqpo

nzk0hqpo1#

好 啊。我修好了。关键是在方法getsessionfactory中,在创建localsessionfactorybean类的对象之后,第一行应该是

factoryBean.setAnnotatedClasses(FileEntity.class);

因此,创建localsessionfactorybean类的对象的正确方法是:

LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

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

相关问题