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

后端 未结 19 1649
滥情空心
滥情空心 2020-12-04 14:59

I have example web application Hibernate 4.3.5 + Derby database 10.10.1.1+ Glassfish4.0 with IDE NetBeans 8.0Beta.

I have the next exception:

Caused          


        
相关标签:
19条回答
  • 2020-12-04 15:38

    Finally I found a mistake! Hope this is useful to someone. When doing a request to the database(in my case it Apache Derby), name of base need write the first letter upper case other in lower case.

    This is wrong query:

    session.createQuery("select first_name from CUSTOMERV").
    

    This is valid query

    session.createQuery("select first_name from Customerv"). 
    

    And class entity must be same name as database, but I'm not sure.

    0 讨论(0)
  • 2020-12-04 15:41

    In my case: spring boot 2 ,multiple datasource(default and custom). entityManager.createQuery go wrong: 'entity is not mapped'

    while debug, i find out that the entityManager's unitName is wrong(should be custom,but the fact is default) the right way:

    @PersistenceContext(unitName = "customer1") // !important, 
    private EntityManager em;
    

    the customer1 is from the second datasource config class:

    @Bean(name = "customer1EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("customer1DataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.xxx.customer1Datasource.model")
                .persistenceUnit("customer1")
                // PersistenceUnit injects an EntityManagerFactory, and PersistenceContext
                // injects an EntityManager.
                // It's generally better to use PersistenceContext unless you really need to
                // manage the EntityManager lifecycle manually.
                // 【4】
                .properties(jpaProperties.getHibernateProperties(new HibernateSettings())).build();
    }
    

    Then,the entityManager is right.

    But, em.persist(entity) doesn't work,and the transaction doesn't work.

    Another important point is:

    @Transactional("customer1TransactionManager") // !important
    public Trade findNewestByJdpModified() {
        //test persist,working right!
        Trade t = new Trade();
        em.persist(t);
        log.info("t.id" + t.getSysTradeId());
    
        //test transactional, working right!
        int a = 3/0;
    }
    

    customer1TransactionManager is from the second datasource config class:

    @Bean(name = "customer1TransactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("customer1EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
    

    The whole second datasource config class is :

    package com.lichendt.shops.sync;
    
    import javax.persistence.EntityManagerFactory;
    import javax.sql.DataSource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
    import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(entityManagerFactoryRef = "customer1EntityManagerFactory", transactionManagerRef = "customer1TransactionManager",
            // 【1】这里写的是DAO层的路径 ,如果你的DAO放在 com.xx.DAO下面,则这里写成 com.xx.DAO
            basePackages = { "com.lichendt.customer1Datasource.dao" })
    public class Custom1DBConfig {
    
        @Autowired
        private JpaProperties jpaProperties;
    
        @Bean(name = "customer1DatasourceProperties")
        @Qualifier("customer1DatasourceProperties")
        @ConfigurationProperties(prefix = "customer1.datasource")
        public DataSourceProperties customer1DataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean(name = "customer1DataSource")
        @Qualifier("customer1DatasourceProperties")
        @ConfigurationProperties(prefix = "customer1.datasource") //
        // 【2】datasource配置的前缀,对应上面 【mysql的yaml配置】
        public DataSource dataSource() {
            // return DataSourceBuilder.create().build();
            return customer1DataSourceProperties().initializeDataSourceBuilder().build();
        }
    
        @Bean(name = "customer1EntityManagerFactory")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                @Qualifier("customer1DataSource") DataSource dataSource) {
            return builder.dataSource(dataSource).packages("com.lichendt.customer1Datasource.model") // 【3】这里是实体类的包路径
                    .persistenceUnit("customer1")
                    // PersistenceUnit injects an EntityManagerFactory, and PersistenceContext
                    // injects an EntityManager.
                    // It's generally better to use PersistenceContext unless you really need to
                    // manage the EntityManager lifecycle manually.
                    // 【4】
                    .properties(jpaProperties.getHibernateProperties(new HibernateSettings())).build();
        }
    
        @Bean(name = "customer1TransactionManager")
        public PlatformTransactionManager transactionManager(
                @Qualifier("customer1EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
            return new JpaTransactionManager(entityManagerFactory);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 15:45

    add parameter nativeQuery = true

    ex: @Query(value="Update user set user_name =:user_name,password =:password where user_id =:user_id",nativeQuery = true)

    0 讨论(0)
  • 2020-12-04 15:47

    If you by any chance using java for configuration, you may need to check the below bean declaration if you have package level changes. Eg: com.abc.spring package changed to com.bbc.spring

    @Bean
        public SessionFactory sessionFactory() {
    
            LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
            //builder.scanPackages("com.abc.spring");    //Comment this line as this package no longer valid.
            builder.scanPackages("com.bbc.spring");
            builder.addProperties(getHibernationProperties());
    
            return builder.buildSessionFactory();
        }
    
    0 讨论(0)
  • 2020-12-04 15:48

    None of the other solution worked for me.

    Even if I don't think its the best practice, I Had to add it into the code like this

    configuration.addAnnotatedClass(com.myOrg.entities.Person.class);

    here

    public static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure();
    
        configuration.addAnnotatedClass(com.myOrg.entities.Person.class);
    
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;
    }
    
    0 讨论(0)
  • 2020-12-04 15:48

    Other persons that are using mapping classes for Hibernate, make sure that have addressed correctly to model package in sessionFactory bean declaration in the following part:

    <property name="packagesToScan" value="com.mblog.model"></property>
    
    0 讨论(0)
提交回复
热议问题