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());
}
}
}
}
我正在尝试使用hibernate映射现有数据库,但是我得到了这个例外,请帮忙。
线程中的异常&#34; main&#34; org.hibernate.hql.ast.QuerySyntaxException:未映射MisRecords [来自MisRecords]
答案 0 :(得分:1)
您似乎忘记在 MisRecords 的hibernate配置中添加映射。
如果您已通过hibernate.cfg.xml手动配置了hibernate上下文,则可以在该文件中添加映射,如下所示:
<?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>
如果您正在运行spring Web框架上下文,则可以通过在spring persistence配置Bean中配置实体管理器工厂来启用@Entity类的自动扫描:
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;
}
...
}
欢呼声, ceth