3

我开始了 spring mvc 项目并尝试在那里使用 JPA。我有类 User 和 BasePersistable 以及由@RooJavaBean 生成的访问器。

import javax.persistence.GeneratedValue;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.roo.addon.javabean.RooJavaBean;

@RooJavaBean
@MappedSuperclass
public abstract class BasePersistable implements Persistable<Long> {

    private static final long serialVersionUID = -3281161320810540373L;

    @Id
    @GeneratedValue
    private Long id;

    @Version
    private Integer version;

    @Override
    public boolean isNew() {
        return getId() == null;
    }

}

@Entity
@RooJavaBean
public class User extends BasePersistable {

    private static final long serialVersionUID = -1999311284585286126L;

    private String username;
    private String passwrd;
    private String firstname;
    private String lastname;
    private String email;

    @Temporal(TemporalType.TIMESTAMP)
    private Calendar signupDate;

    private Sex sex;
    private String comment;

}

Roo 肯定会生成 getter 和 setter。我可以在代码中访问 getId() 方法:

User user = new User();
user.getId();

但我得到了这个错误:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.test:mvc1:war:1.0.0-BUILD-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.codehaus.mojo:aspectj-maven-plugin @ line 284, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mvc1 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ mvc1 ---
[INFO] Deleting /home/zzz/ws_java/mvc1/target
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ mvc1 ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ mvc1 ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 6 source files to /home/zzz/ws_java/mvc1/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/zzz/ws_java/mvc1/src/main/java/com/test/mvc1/domain/User.java:[13,7] error: User is not abstract and does not override abstract method getId() in Persistable
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.831s
[INFO] Finished at: Sat Aug 11 09:56:58 NOVT 2012
[INFO] Final Memory: 16M/86M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project mvc1: Compilation failure
[ERROR] /home/zzz/ws_java/mvc1/src/main/java/com/test/mvc1/domain/User.java:[13,7] error: User is not abstract and does not override abstract method getId() in Persistable
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我认为 AspectJ 似乎存在一些问题。任何人都可以帮忙吗?

UPD

这是 BasePersistable_Roo_JavaBean.aj

// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
// You may push code into the target .java compilation unit if you wish to edit any member(s).

package com.test.mvc1.domain;

import com.test.mvc1.domain.BasePersistable;

privileged aspect BasePersistable_Roo_JavaBean {

    public Long BasePersistable.getId() {
        return this.id;
    }

    public void BasePersistable.setId(Long id) {
        this.id = id;
    }

    public Integer BasePersistable.getVersion() {
        return this.version;
    }

    public void BasePersistable.setVersion(Integer version) {
        this.version = version;
    }

}

顺便说一句,如果将存根方法 getId() 添加到 User 类:

public Long getId() {
        return 1L; 
}

UPD 2 我什至无法想象这个项目有什么问题。我在 pom 文件中添加了新的依赖项,maven 不下载它。这是我的pom:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>mvc1</artifactId>
    <name>mvc1</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.7</java-version>
        <spring.version>3.1.0.RELEASE</spring.version>
        <aspectj.version>1.6.12</aspectj.version>
        <org.slf4j-version>1.5.10</org.slf4j-version>
        <roo.version>1.2.1.RELEASE</roo.version>
        <data-jpa.version>1.1.0.M1</data-jpa.version>
        <tiles.version>2.2.2</tiles.version>
        <hibernate-jpa-api.version>1.0.1.Final</hibernate-jpa-api.version>
        <hibernate-entity-manager.version>4.1.0.Final</hibernate-entity-manager.version>
        <aspectj-plugin.version>1.2</aspectj-plugin.version>
        <eclipse-plugin.version>2.7</eclipse-plugin.version>
        <compiler-plugin.version>2.3.2</compiler-plugin.version>
        <!-- database properties start -->
        <database.url>jdbc:mysql://localhost:3306/mvctest</database.url>
        <database.username>root</database.username>
        <database.password />
        <database.driverClassName>com.mysql.jdbc.Driver</database.driverClassName>
        <database.type>MYSQL</database.type>
        <!-- database properties end -->
    </properties>

    <repositories>
        <repository>
            <id>spring-maven-release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </repository>
        <repository>
            <id>spring-maven-milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </repository>
        <repository>
            <id>spring-roo-repository</id>
            <name>Spring Roo Repository</name>
            <url>http://spring-roo-repository.springsource.org/release</url>
        </repository>
    </repositories>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${data-jpa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <!-- Hibernate/Jpa -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-entity-manager.version}</version>
        </dependency>

        <!-- Database -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
        </dependency>

        <!-- Roo -->
        <dependency>
            <groupId>org.springframework.roo</groupId>
            <artifactId>org.springframework.roo.annotations</artifactId>
            <version>${roo.version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>   
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>${tiles.version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
          <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.properties</include>
            </includes>
          </resource>
          <resource>
            <directory>src/main/resources</directory>
            <excludes>
              <exclude>**/*.properties</exclude>
            </excludes>
          </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <!-- version is used because of Roo 1.2.1.RELEASE requirement -->
                <!-- remove it when fixed -->
                <version>${eclipse-plugin.version}</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                    <additionalBuildcommands>
                        <buildCommand>
                            <name>org.eclipse.ajdt.core.ajbuilder</name>
                            <arguments>
                                <aspectPath>org.springframework.aspects</aspectPath>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>org.springframework.ide.eclipse.core.springbuilder</name>
                        </buildCommand>
                    </additionalBuildcommands>
                    <additionalProjectnatures>
                        <projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
                        <projectnature>com.springsource.sts.roo.core.nature</projectnature>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>${compiler-plugin.version}</version>
              <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>aspectj-maven-plugin</artifactId>
              <version>${aspectj-plugin.version}</version>
              <dependencies>
                <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjrt</artifactId>
                  <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjtools</artifactId>
                  <version>${aspectj.version}</version>
                </dependency>
              </dependencies>
              <executions>
                <execution>
                  <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                  </goals>
                </execution>
              </executions>
              <configuration>
                <outxml>true</outxml>
                <aspectLibraries>
                  <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                  </aspectLibrary>
                </aspectLibraries>
                <source>1.6</source>
                <target>1.6</target>
              </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.4.v20120524</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
                    <excludes>
                        <exclude>**/*_Roo_*</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的 servlet 上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

     <!-- ========================= GENERAL DEFINITIONS ========================= -->

    <mvc:annotation-driven />
    <mvc:resources location="/resources/" mapping="/resources/**"/>

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
    <context:component-scan base-package="com.test.mvc1.web">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <jpa:repositories base-package="com.test.mvc1.repository" />

    <!-- ========================= VIEW DEFINITIONS ========================= -->

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    </bean>

    <!-- ========================= TILES DEFINITIONS ========================= -->

    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/defs/tiles.xml</value>
            </list>
        </property>
    </bean>

    <!-- ========================= Persistence configuration ========================= -->

    <jpa:repositories base-package="com.test.mvc1.repository" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />

        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="validationQuery" value="SELECT NOW()" />
    </bean>

    <bean id="entityManagerFactory" depends-on="liquibase" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.test.mvc1.domain, ${database.packagesToScan}" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">${database.namingStrategy}</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.show_sql">${database.showsql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${database.hbm2ddl}</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            </props>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="${database.type}" />
                <property name="showSql" value="${database.showsql}" />
                <property name="generateDdl" value="false" />
            </bean>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
        <property name="changeLog" value="classpath:META-INF/db/changelog.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

这是错误:

2012-08-12 10:42:24.250:WARN:/:unavailable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
Caused by: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
Caused by: 
javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
Caused by: 
org.hibernate.AnnotationException: No identifier specified for entity: com.test.mvc1.domain.User
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:276)
2012-08-12 10:42:24.525:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

我需要帮助!我想问题是某些依赖项或插件中的版本冲突。

4

1 回答 1

0

编辑:将答案更新为预先修改的问题...

org.hibernate.AnnotationException: No identifier specified for entity: com.malykhinvi.mvc1.domain.User

所以这里的错误表明您的实体com.malykhinvi.mvc1.domain.User没有@Id描述主键的注释。

你可以尝试放置BasePersistable

 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
于 2012-08-11T03:41:29.990 回答