簡體   English   中英

帶有Hibernate映射的BeanCreationException(使用Spring和Maven)

[英]BeanCreationException with Hibernate mapping (using Spring and Maven)

我正在嘗試正確地建立不同實體之間的關系。

看着代碼,我認為一切正常且一致(我已經驗證了這些屬性具有與在Hibernate映射配置文件中建立的名稱相同的名稱),但是,它不能編譯。 因此很明顯,這不是很好。 顯示的錯誤如下:

org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'org.springframework.dao.annotation.
PersistenceExceptionTranslationPostProcessor#0' defined in class path
resource [spring.cfg.xml]: Initialization of bean failed; nested 
exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in class path
resource [spring.cfg.xml]: Invocation of init method failed; nested
exception is org.hibernate.HibernateException: Unable to instantiate
default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

pom.xml文件是以下

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.igalia.mswl</groupId>
<artifactId>snippr</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>snippr Maven Webapp</name>
<url>http://maven.apache.org</url>

<!-- =================================================================== -->
<!-- Default values for properties.These default values are expected to
    be valid for most profiles.Specific profiles can overwrite values when necessary. -->
<properties>
    <!-- Data source properties -->
    <dataSource.user>test</dataSource.user>
    <dataSource.password>test</dataSource.password>
    <dataSource.jndiName>jdbc/testdb</dataSource.jndiName>
    <testDataSource.user>${dataSource.user}</testDataSource.user>
    <testDataSource.password>${dataSource.password}</testDataSource.password>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    <spring-security.version>3.0.5.RELEASE</spring-security.version>
</properties>

<!-- =================================================================== -->
<!-- Profiles. * The build is always executed by selecting at least two
    non-exclusive profiles. By default, such profiles are "dev" and "postgresql"
    (meaning "use PostgreSQL assuming a development environment"). * General
    profiles. There are two general (database-independent) profiles: "dev" and
    "prod". The former is used for development (including testing) and the latter
    is used for production (including testing). As shown below, two dataSources
    (databases schemas) are used in both profiles: one for running (dataSource)
    and another one for the Maven test fase (testDataSource). Note the Maven
    test fase is executed both with development and production profiles. * Database-specific
    profiles. There is a profile for each supported database. * Specific profiles
    can be defined to better adapt to a particular environment by overwriting/adding
    properties and/or including other chunks of valid XML. * Usage: + mvn <<goal>>
    => Execute <<goal>> with default profiles. + mvn -Pdev,<<database>> <<goal>
    => Execute <<goal>> with "dev" and <<database>> profiles. + mvn -Pprod,<<database>>
    <<goal>> => Execute <<goal>> with "prod" and <<database>> profiles. + Note
    that when using -P option all desired profiles must be specified (e.g. "-Pprod"
    with the intention to select "prod" and the default database profile is not
    correct; "-Pprod,<<database>>" must be used instead). * Examples: + mvn <<goal>>
    + mvn -Ppostgresql,prod <<goal>> + mvn -Ppostgresql,dev <<goal>> -->

<profiles>
    <!-- Development profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- SnippR environment properties -->
            <snippr.mode>dev</snippr.mode>
            <!-- Hibernate properties -->
            <hibernate.show_sql>true</hibernate.show_sql>
            <hibernate.format_sql>true</hibernate.format_sql>
            <hibernate.use_sql_comments>true</hibernate.use_sql_comments>
            <hibernate.hbm2ddl.auto>update</hibernate.hbm2ddl.auto>
        </properties>
    </profile>

    <!-- MySQL profile -->
    <profile>
        <id>mysql</id>
        <properties>
            <!-- JDBC driver properties -->
            <jdbcDriver.groupId>mysql</jdbcDriver.groupId>
            <jdbcDriver.artifactId>mysql-connector-java</jdbcDriver.artifactId>
            <jdbcDriver.version>5.0.5</jdbcDriver.version>
            <jdbcDriver.className>com.mysql.jdbc.Driver</jdbcDriver.className>
            <!-- Data source properties -->
            <dataSource.url>jdbc:mysql://localhost/testdb</dataSource.url>
            <!-- Hibernate properties -->
            <hibernate.dialect>org.hibernate.dialect.MySQLInnoDBDialect</hibernate.dialect>
        </properties>
    </profile>

    <!-- PostgreSQL profile -->
    <profile>
        <id>postgresql</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- JDBC driver properties -->
            <jdbcDriver.groupId>postgresql</jdbcDriver.groupId>
            <jdbcDriver.artifactId>postgresql</jdbcDriver.artifactId>
            <jdbcDriver.version>8.3-603.jdbc4</jdbcDriver.version>
            <jdbcDriver.className>org.postgresql.Driver</jdbcDriver.className>
            <!-- Data source properties -->
            <dataSource.url>jdbc:postgresql://localhost/testdb</dataSource.url>
            <!-- Hibernate properties -->
            <hibernate.dialect>org.hibernate.dialect.PostgreSQLDialect</hibernate.dialect>
            <!-- <databasetable.prefix>public.</databasetable.prefix> -->
        </properties>
    </profile>

</profiles>

<!-- =================================================================== -->
<!-- Repository management -->
<repositories>
    <repository>
        <id>zkoss</id>
        <url>http://mavensync.zkoss.org/maven2/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
    <repository>
        <id>central</id>
        <url>http://repo1.maven.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

<!-- =================================================================== -->
<!-- Dependency management -->
<dependencies>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.4</version>
        <scope>test</scope>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

    <!-- ZK -->
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zk</artifactId>
        <version>5.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zul</artifactId>
        <version>5.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zkplus</artifactId>
        <version>5.0.11</version>
    </dependency>

    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zkspring-core</artifactId>
        <version>3.0</version>
    </dependency>
    <dependency>
        <groupId>org.zkoss.zk</groupId>
        <artifactId>zkspring-security</artifactId>
        <version>3.0</version>
    </dependency>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-jta_1.0.1B_spec</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>

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

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.5.4</version>
    </dependency>

    <!-- JDBC driver -->
    <dependency>
        <groupId>${jdbcDriver.groupId}</groupId>
        <artifactId>${jdbcDriver.artifactId}</artifactId>
        <version>${jdbcDriver.version}</version>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring-security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring-security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-acl</artifactId>
        <version>${spring-security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring-security.version}</version>
    </dependency>
</dependencies>

<build>
    <finalName>snippr</finalName>

    <!-- =============================================================== -->
    <!-- Filtering -->
    <resources>

        <!-- Apply filtering to files matching the following expressions in src/main/resources. -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>*spring.cfg.xml</include>
                <include>*hibernate.cfg.xml</include>
                <include>jetty-env.xml</include>
            </includes>
        </resource>

        <!-- Continue considering resources the files in src/main/resources, but
            without applying filtering. -->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

    <testResources>
        <!-- Apply filtering to files matching the following expressions in src/test/resources. -->
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>*spring.cfg-test.xml</include>
                <include>*hibernate.cfg-test.xml</include>
            </includes>
        </testResource>

        <!-- Continue considering resources the files in src/test/resources, but
            without applying filtering. -->
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>

    <!-- Maven plugin -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <verbose>true</verbose>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.18</version>
            <configuration>
                <jettyEnvXml>target/classes/jetty-env.xml</jettyEnvXml>
                <reload>manual</reload>
                <stopPort>9966</stopPort>
                <stopKey>stop</stopKey>

                <!-- Log to the console. -->
                <requestLog implementation="org.mortbay.jetty.NCSARequestLog">
                    <append>true</append>
                </requestLog>
            </configuration>

            <dependencies>
                <dependency>
                    <groupId>${jdbcDriver.groupId}</groupId>
                    <artifactId>${jdbcDriver.artifactId}</artifactId>
                    <version>${jdbcDriver.version}</version>
                </dependency>
            </dependencies>

        </plugin>

    </plugins>

</build>
</project>

Snippr.hbm.xml是以下內容之一:

<?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 package="org.snippr.business.entities" default-access="field">

<!-- User -->
<class name="User" table="users">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="firstName" column="first_name" />
    <property name="lastName" column="last_name" />
    <property name="username" column="user_name" unique="true" />
    <property name="password" column="password" />
    <property name="email" column="email" />
    <property name="enabled" column="enabled" />
    <property name="accountNonExpired" column="account_non_expired" />
    <property name="credentialsNonExpired" column="credentials_non_expired" />
    <property name="accountNonLocked" column="account_non_locked" />

    <set name="roles" table="users_roles" lazy="false">
        <key column="user_id" />
        <many-to-many column="role_id" entity-name="org.snippr.business.entities.Role" />
    </set>
    <set name="snippets" inverse="true" cascade="all-delete-orphan" >
        <key column="user_id" />
        <one-to-many class="org.snippr.business.entities.Snippet" />
    </set>
    <set name="labels" inverse="true" cascade="all-delete-orphan" >
        <key column="user_id" />
        <one-to-many class="org.snippr.business.entities.Label" />
    </set>
    <set name="comments" inverse="true"  cascade="all-delete-orphan" >
        <key column="user_id" />
        <one-to-many class="org.snippr.business.entities.Comment" />
    </set>
</class>

<!-- Role -->
<class name="Role" table="roles">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="roleName" column="role_name" />
</class>

<!-- Snippet -->
<class name="Snippet" table="snippet">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="title" unique="true" />
    <property name="description" />
    <set name="snippetCodes" inverse="true" cascade="all-delete-orphan">
        <key column="snippet_id" />
        <one-to-many class="org.snippr.business.entities.SnippetCode" />
    </set>
    <many-to-one name="user" class="org.snippr.business.entities.User"
        column="user_id" not-null="true" />
    <many-to-one name="label" class="org.snippr.business.entities.Label"
        column="label_id" not-null="true" />
</class>

<!-- SnippetCode -->
<class name="SnippetCode" table="snippet_code">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="code" unique="false" type="text" />
    <many-to-one name="snippet" class="org.snippr.business.entities.Snippet"
        column="snippet_id" not-null="true" />
</class>

<!-- Label -->
<class name="Label" table="label">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="name" unique="false" />

    <many-to-one name="user" class="org.snippr.business.entities.User"
        column="user_id" not-null="true" />
    <set name="snippets" inverse="true" cascade="save-update">
        <key column="label_id" />
        <one-to-many class="org.snippr.business.entities.Snippet" />
    </set>
</class>

<!-- Comment -->
<class name="Comment" table="comment">
    <id name="id" access="property">
        <generator class="native" />
    </id>

    <property name="text" column="text"/>
    <property name="email" column="email"/>
    <property name="url" column="url"/>

    <many-to-one name="user" class="org.snippr.business.entities.User"
        column="user_id" not-null="true" />
</class>

</hibernate-mapping>

Maven下載的.jar文件如下

  • antlr-2.7.6.jar aopalliance-1.0.jar Aspectjrt-1.6.8.jar
  • Aspectjtools-1.5.4.jar Aspectjweaver-1.6.8.jar bsh-2.0b4.jar
  • commons-collections-3.1.jar commons-fileupload-1.2.1.jar
  • commons-logging-1.1.1.jar dom4j-1.6.1.jar
  • geronimo-jta_1.0.1B_spec-1.1.1.jar
  • hibernate-commons-annotations-3.2.0.Final.jar
  • hibernate-core-3.6.5.Final.jar hibernate-jpa-2.0-api-1.0.0.Final.jar
  • javassist-3.12.1.GA.jar jta-1.1.jar log4j-1.2.16.jar
  • postgresql-8.3-603.jdbc4.jar slf4j-api-1.6.1.jar
  • spring-aop-3.0.3.RELEASE.jar spring-asm-3.0.5.RELEASE.jar
  • 彈簧豆-3.0.5.RELEASE.jar彈簧上下文3.0.5.RELEASE.jar
  • spring-context-support-3.0.3.RELEASE.jar
  • 彈簧芯3.0.5.RELEASE.jar彈簧表達-3.0.3.RELEASE.jar
  • spring-jdbc-3.0.5.RELEASE.jar spring-orm-3.0.5.RELEASE.jar
  • spring-security-acl-3.0.5.RELEASE.jar
  • 彈簧安全配置-3.0.5.RELEASE.jar
  • 春季安全核心-3.0.5.RELEASE.jar
  • spring-security-taglibs-3.0.5.RELEASE.jar
  • spring-security-web-3.0.5.RELEASE.jar
  • 彈簧測試-3.0.5.RELEASE.jar
  • spring-tx-3.0.5.RELEASE.jar spring-web-3.0.5.RELEASE.jar
  • zcommon-5.0.11.jar zcommons-el-1.1.0.jar zk-5.0.11.jar
  • zkplus-5.0.11.jar zkspring-core-3.0.jar zkspring-security-3.0.jar
  • zul-5.0.11.jar zweb-5.0.11.jar

任何Java專家都能看到這里發生的事情或提供任何線索嗎? 正如我所說,我已經驗證了名稱屬性,並且看不到任何拼寫錯誤。

有時,當您的類路徑中沒有javassist.jar時,會發生此錯誤。 如我所見,你宣布了這一點。 確保您具有類屬性的所有獲取器和設置器。

媽的! 我真的很傻。 我確信我已經驗證了實例的所有屬性。

該錯誤是由於一個名為“ Comment”的類中的錯誤引起的。 我已經閱讀了堆棧跟蹤中的最新錯誤,但是如果繼續閱讀堆棧跟蹤,我可以找到以下內容:

Caused by: org.hibernate.PropertyNotFoundException: field [user] not found on org.snippr.business.entities.Comment
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:189)
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174)
at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:197)
at org.hibernate.mapping.Property.getGetter(Property.java:304)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:297)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:155)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)

類Comment中沒有屬性“ user”。 映射和類Comment之間存在不一致。 映射引用屬性user,但是Comment具有屬性Set users 該類是不正確的(我用錯誤的意義建立了1-N關系)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM