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]
It looks like you have forgotten to add a mapping in the hibernate configuration for your MisRecords .
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>
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.