org.hibernate.MappingException: Unknown entity: annotations.Users

前端 未结 19 1489
情歌与酒
情歌与酒 2020-12-01 07:11

Consider the hierarchy :

\"enter

And the following classes and xml :

相关标签:
19条回答
  • 2020-12-01 07:58

    Check the package name is given in the property packagesToScan in the dispatcher-servlet.xml

    <bean id="sessionFactory"             class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
            <property name="dataSource" ref="dataSource" />  
            <property name="packagesToScan" value="**entity package name here**"></property> 
            <property name="hibernateProperties">  
                <props>  
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
                </props>  
            </property>  
        </bean>
    
    0 讨论(0)
  • 2020-12-01 07:58

    Problem occurs because of the entry mapping class="annotations.Users" in hibernate.cfg.xml remove that line then it will be work.

    I had the same issue. When I removed the above line then it was working fine for me.

    0 讨论(0)
  • 2020-12-01 07:59

    If you are using 5.0x version,configuration with standard service registry is deprecated.

    Instead you should bootstrap it with Metadata: In your HibernateUtil class, you should add

    private static SessionFactory buildSessionFactory() {
        try {           
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                .configure( "hibernate.cfg.xml" )
                .build();
    
            Metadata metadata = new MetadataSources( standardRegistry )
                .getMetadataBuilder()
                .build();
    
            return metadata.getSessionFactoryBuilder().build();
        } catch(...) {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:01

    If your entity is mapped through annotations, add the following code to your configuration;

    configuration.addAnnotatedClass(theEntityPackage.EntityClassName.class);
    

    For example;

    configuration.addAnnotatedClass(com.foo.foo1.Products.class);
    

    if your entity is mapped with xml file, use addClass instead of addAnnotatedClass.

    As an example;

    configuration.addClass(com.foo.foo1.Products.class);
    

    Ping me if you need more help.

    0 讨论(0)
  • 2020-12-01 08:01

    I was facing the same issue and I searched for almost 2 hours and tried with different possible ways like replacing old hibernate JARs and changing the DB table schema. But finally got the solution as below:

    SessionFactory factory = new Configuration().configure().buildSessionFactory(); //This line to be replaced with below commented line
    

    Replace above for

    //Configuration config = new Configuration().configure();
    //ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
    //SessionFactory factory = config.buildSessionFactory(servReg);
    

    It will then work fine..

    0 讨论(0)
  • 2020-12-01 08:03

    Initially I trying with the below code which didn't work for me

    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    Metadata metadata = metadataSources.getMetadataBuilder().build();
    SessionFactory sessionFactory= metadata.getSessionFactoryBuilder().build();
    

    For me below configuration worked. All the hibernate properties i provided from the code itself and using hibernate version 5+. I'm trying to connect to the Postgressql db.

    imports:

     import org.hibernate.Session;
     import org.hibernate.SessionFactory;
     import org.hibernate.Transaction;
     import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
     import org.hibernate.cfg.Configuration;
     import org.hibernate.cfg.Environment;
     import org.hibernate.service.ServiceRegistry;
    

    code:

        Configuration configuration = new Configuration();
        configuration.setProperty("hibernate.current_session_context_class", "thread");
        configuration.setProperty(Environment.DRIVER, "org.postgresql.Driver");
        configuration.setProperty(Environment.URL, lmsParams.getDbProperties().getDbURL());
        configuration.setProperty(Environment.USER, lmsParams.getDbProperties().getUsername());
        configuration.setProperty(Environment.PASS, lmsParams.getDbProperties().getPassword());
    
        configuration.setProperty("hibernate.connection.release_mode", "auto");
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        configuration.setProperty("hibernate.show_sql", "true");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create");
        configuration.addAnnotatedClass(LMSSourceTable.class);
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    

    Latest Update: One more option worked for me using the Reflections package as a dependency. This i tried with both mysql and postgress and it works fine.

    Some info about Reflections: The Reflections library works as a classpath scanner. It indexes the scanned metadata and allows us to query it at runtime. It can also save this information, so we can collect and use it at any point during our project, without having to re-scan the classpath again

    if you are using maven:

    <dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
    </dependency>
    

    code:

     MetadataSources metadataSources = new 
     MetadataSources(serviceRegistry);
     Reflections entityPackageReflections = new Reflections("com.company.abc.package.name");
        entityPackageReflections.getTypesAnnotatedWith(Entity.class).forEach(metadataSources::addAnnotatedClass);
        Metadata metadata = metadataSources.getMetadataBuilder().build();
        SessionFactory sessionFactory=  metadata.getSessionFactoryBuilder().build();
    
    0 讨论(0)
提交回复
热议问题