我正在尝试正确建立不同实体之间的关系。
观察代码我认为一切都很好并且连贯(我已经验证了属性与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文件如下
任何Java专家都能看到这里发生的事情或提供任何线索?正如我所说,我已经验证了名称属性,但我没有看到任何拼写错误。
答案 0 :(得分:0)
有时在类路径中没有javassist.jar时会发生此错误。正如我看到你宣布的那样。确保您拥有类属性的所有getter和setter。
答案 1 :(得分:0)
该错误是由于其中一个名为“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之间存在不一致。 映射是指属性用户,但是注释具有属性Set users 。这个类是不正确的(我在错误的意义上建立了1-N的关系)。