0

MisRecords.java

@Entity
    @Table(name="dat_emprecords")
    public class MisRecords {
    @Id
    @GeneratedValue
    @Column(name="pk_EmpRec_Idx")
        int id;
        @Column(name="EmpRec_EmpFName")
        String firstName;
        @Column(name="EmpRec_EmpLName")
        String lastName;
        @Column(name="fk_EmpRec_EmpID")
        int empId;
        @Column(name="fk_emprec_empreportingmgrid")
        int empReportingManagerId;
        // setters and getters

Test.java

public class Test {
public static void main(String[] args) {
Session session=new  AnnotationConfiguration().configure().buildSessionFactory().openSession();
 Query query=session.createQuery("from MisRecords");
    List<MisRecords> list=query.list();{
    for(MisRecords employee:list){
          System.out.println(employee.getFirstName());
     }
 }
}    
}

I am trying to map an existing database using hibernate but I get this exception, please help.

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: MisRecords is not mapped [from MisRecords]

1
  • Either the entity is not listed in the Hibernate configuration file, or it's not annotated with the correct Entity annotation (javax.persistence.Entity). Commented Jun 1, 2015 at 11:38

1 Answer 1

1

It looks like you have forgotten to add a mapping in the hibernate configuration for your MisRecords.

Variant a) hibernate.cfg.xml with separate mapping file:

If you have configured your hibernate context manually by hibernate.cfg.xml you have add your mapping in that file as seen below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <!-- Default Database Dialect in Hibernate -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--  Schema -->
        <property name="hibernate.default_schema">YOUR_DB_SCHEME</property>

        ...

        <!-- Your mapping for your model goes after -->
        <mapping class="coolproject.modelpackage.ModelEntity1"/>
        <mapping class="coolproject.modelpackage.ModelEntity2"/>
        <mapping class="coolproject.modelpackage.ModelEntity3"/>
        <mapping class="coolproject.modelpackage.ModelEntity4"/>

    </session-factory>
</hibernate-configuration>

Variant b) Spring ORM with JPA

If you have a spring web framework context running, you could enable automated scanning for @Entity classes by configuring an entity manager factory in a spring persistence configuration bean:

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig {

    ...

    /**
     * EntityManager Factory Bean
     * 
     * @return
     *      {@link LocalContainerEntityManagerFactoryBean}
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());

        // add base packages containing @Entity annotated classes here
        em.setPackagesToScan(new String[] { "packages", "to.scan", "for.Entity.annotated.classes" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }

    ...

}

cheers, ceth

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.