org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 使用PostgreSQL JDBC的«ResultSet»Hibernate中不存在名称为«name»的列

时间:2016-02-11 00:55:31

标签: hibernate postgresql hibernate-native-query

正如主题所说,我在Hibernate 5中使用PostgreSQL DB时出现了这个错误。

Hibernate返回给我的查询是: utenti(email (pk), password, nome, cognome, datanascita, sesso) (所以这是正确的) 表格是:

Unable to load configuration. - action - file:<path>/NetBeansProjects/DBIIProject/target/DBIIProject-1.0-SNAPSHOT/WEB-INF/classes/struts.xml:15:34

此外,我收到另一个错误: 异常启动过滤器struts2

struts.xml

我的 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <!-- Cambio l'estensione delle Action da .action a .do --> <constant name="struts.action.extension" value="do" /> <package name="default" extends="struts-default" namespace="/"> <action name="Login" class="org.altervista.cdpjavaexamples.dbiiproject.actions.LoginAction" method="execute"> <result name="success">index.jsp</result> <result name="input">login.jsp</result> </action> <action name="Logout" class="org.altervista.cdpjavaexamples.dbiiproject.actions.LogoutAction" method="execute"> <result name="success">index.jsp</result> </action> <action name="PasswordRecovery" class="org.altervista.cdpjavaexamples.dbiiproject.actions.PasswordRecoveryAction" method="execute"> <result name="success">password_recovery.jsp</result> <result name="input">password_recovery.jsp</result> </action> </package> </struts> 是:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.altervista.cdpjavaexamples</groupId>
    <artifactId>DBIIProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>DBIIProject</name>

    <properties>
        <struts2.version>2.3.24.1</struts2.version>
        <log4j2.version>2.2</log4j2.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-taglib</artifactId>
            <version>1.3.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.24.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1207.jre7</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts2.version}</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
                <configuration>
                    <stopKey>CTRL+C</stopKey>
                    <stopPort>8999</stopPort>
                    <systemProperties>
                        <systemProperty>
                            <name>xwork.loggerFactory</name>
                            <value>com.opensymphony.xwork2.util.logging.log4j2.Log4j2LoggerFactory</value>
                        </systemProperty>
                    </systemProperties>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webAppSourceDirectory>${basedir}/src/main/webapp/</webAppSourceDirectory>
                    <webAppConfig>
                        <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
                    </webAppConfig>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
    <property name="hibernate.connection.username">test</property>
    <property name="hibernate.connection.password">test</property>
    <property name="hibernate.show_sql">true</property>
    <mapping class="" file="" jar="" package="" resource="org/altervista/cdpjavaexamples/dbiiproject/model/Utenti.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

最后我的Hibernate文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <!-- Mappatura classe - tabella -->
  <class name="org.altervista.cdpjavaexamples.dbiiproject.model.Utenti" table="utenti">
    <meta attribute="class-description">
          Descrizione della classe
      </meta>
    <!-- Chiave primaria -->
    <id column="email" name="email" type="text"/>
    <!-- Campi -->
    <property column="nome" name="nome" not-null="true" type="text"/>
    <property column="cognome" name="cognome" not-null="true" type="text"/>
    <property column="password" name="password" not-null="true" type="text"/>
    <property column="sesso" name="sesso" not-null="true" type="boolean"/>
    <property column="datanascita" name="dataNascita" not-null="true" type="date"/>
  </class>

  <!-- Query -->
  <sql-query name="selectEmailPassword">
      <return class="org.altervista.cdpjavaexamples.dbiiproject.model.Utenti"/>
      <![CDATA[
        SELECT email, password FROM utenti WHERE email = :email
      ]]>
  </sql-query>
  <sql-query name="selectNomeCognomePassword">
      <return class="org.altervista.cdpjavaexamples.dbiiproject.model.Utenti"/>
      <![CDATA[
        SELECT nome, cognome, password FROM utenti WHERE email = :email
      ]]>
  </sql-query>
  <sql-query name="emailExists">
      <return class="org.altervista.cdpjavaexamples.dbiiproject.model.Utenti"/>
      <![CDATA[
        SELECT * FROM utenti WHERE email = :email
      ]]>
  </sql-query>
  <!-- Fine query -->
</hibernate-mapping>

Class Building {
   private String name;
   private List<String> facilities;

   public Building(String name, String... facilities) { // facilities won't be null this way
       this.name = name;
       this.facilities = new ArrayList<String>(Arrays.asList(facilities));
   }

   public Building(String name) {
       this(name, new String[0]);
   }

   // Only if this makes sense
   public Building() {
       this("");
   }

   public String[] getFacilities() {
       return facilities.toArray(new String[facilities.size()]);
   }

   // if needed you can...
   public void addFacility(String facility) { ... }
   public void removeFacility(String facility) { ... }
}

Class OfficeBuilding extends Building {
    public OfficeBuilding() {
        super("Office", "facility1", "facility2");
        //...
    }
}

Project's structure

我已经在论坛中搜索过,但我找到的解决方案并不能解决我的问题。

3 个答案:

答案 0 :(得分:0)

您定义<return class="org.altervista.cdpjavaexamples.dbiiproject.model.Utenti"/>,看起来该类具有name属性(例如标记为使用“名称”列)。执行SQL时,hibernate会尝试填充实体,但ResultSet没有列来填充名称。

答案 1 :(得分:0)

您在使用<sql-query>的Hibernate映射中使用了本机SQL查询。这种查询必须使用特定于可以执行此查询的数据库的本机SQL方言。因此,如果您在SQL代码中出错或更改数据库,您的查询可能无法正常工作。在您的代码中没有必要使用本机SQL查询,它们应该替换为named queries

  

可以使用<query>节点配置映射文档。   如果您的查询包含字符,请务必使用CDATA部分   可以解释为标记。

以下是此类查询的示例

<query name="selectEmailPassword"><![CDATA[
    from org.altervista.cdpjavaexamples.dbiiproject.model.Utenti as u
    WHERE u.email = :email
]]></query> 

参数绑定

Query q = sess.getNamedQuery("selectEmailPassword");
q.setString("email", email);
List<Utenti> l = q.list();

答案 2 :(得分:0)

感谢所有人。最后,我使用Criteria对象来执行查询。