2017-04-26 67 views
0

我目前在一家没有主应用程序文档的公司工作,所以我必须在每次必须做的更改中使用尝试和失败技术。如何添加新的Java类以便与数据库进行映射?

此应用程序是在JDK 6和一些辅助工具(如RichFaces和Hibernate)的JSF 1.2上开发的,连接到数据库引擎MS SQL Server。此刻,我必须添加一个新的选择,必须从数据库饲料,但每当我执行下面的代码行:

return this.entityManager.createQuery("select d from DiagnosticoPrueba d").getResultList(); 

它显示了以下错误:

org.hibernate.hql.ast.QuerySyntaxException: DiagnosticoPrueba is not mapped [select d from DiagnosticoPrueba d] 

我使用的过程添加新的选择器如下:

  1. 我创建了DIAGNOSTICO表并插入了相应的数据。为了证实这些信息,我会告诉你下列句子的结果。

    SELECT * FROM DIAGNOSTICO; 
    

enter image description here

EXEC sp_columns DIAGNOSTICO; 

enter image description here

  • 我创建JavaDiagnosticoPrueba类,将映射DIAGNOSTICO表

    package *.entidades; 
    
    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.Id; 
    import javax.persistence.Table; 
    
    @Entity (name = "DiagnosticoPrueba") 
    @Table(name = "DIAGNOSTICO") 
    public class DiagnosticoPrueba implements java.io.Serializable { 
    
    private static final long serialVersionUID = 1L; 
    private int id; 
    private String nombre; 
    
    public DiagnosticoPrueba() { 
    } 
    
    public DiagnosticoPrueba(int id, String nombre) { 
        this.id = id; 
        this.nombre = nombre; 
    } 
    
    @Id 
    @Column(name = "ID", unique = true, nullable = false) 
    public int getId() { 
        return id; 
    } 
    
    public void setId(int id) { 
        this.id = id; 
    } 
    
    @Column(name = "NOMBRE", length = 200) 
    public String getNombre() { 
        return nombre; 
    } 
    
    public void setNombre(String nombre) { 
        this.nombre = nombre; 
    } 
    } 
    
  • 文件hibernate.cfg.xml不能在申请中找到,但该文件persistence.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> 
        <persistence-unit name="MY_ENTERPRISE" transaction-type="JTA"> 
         <provider>org.hibernate.ejb.HibernatePersistence</provider> 
         <jta-data-source>myEnterpriseDataSource</jta-data-source> 
         <properties> 
          <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/> 
          <property name="hibernate.default_catalog" value="MY_ENTERPRISE"/> 
          <property name="hibernate.default_schema" value="SCHEMA_MY_ENTERPRISE"/> 
          <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> 
          <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener"/> 
          <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener"/> 
          <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener"/> 
          <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener"/> 
          <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener"/> 
          <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener"/> 
         </properties> 
        </persistence-unit> 
    </persistence> 
    

    我认为错误是由于没有从应用程序配置文件中的实体DiagnosticoPrueba是,我认为解决方案的关键在于myEnterpriseDataSource

    你会推荐我做什么?

    谢谢!

    回答

    0

    您需要映射persistence.xml中的类

    设置外部属性部分。

    <class>com.mycompany.entities.DiagnosticoPrueba</class> 
    
    相关问题