我创建了一个带有父 pom 的多模块 spring boot 项目。在模块测试数据中,我添加了一些实体类用户和登录:
@Table
@Entity
public class Login implements Serializable {
... another column
@Column(unique = true, columnDefinition = "VARCHAR(50)", nullable = false)
private String username;
@JsonIgnore
@OneToOne(mappedBy = "login")
private User user;
// getter and setter
用户类:
@Table
@Entity
public class User implements Serializable{
...
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "login_id")
private Login login;
...
模块test-data的pom.xml:
<parent>
<groupId>com.emo.test</groupId>
<artifactId>emo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/>
</parent>
<artifactId>test-data</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
// other dependencies
</dependencies>
测试数据的主要类别:
package com.test;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
test-oauth的主要类:
package com.oauth.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OAuthApplication {
public static void main(String[] args) {
SpringApplication.run(OAuthApplication.class, args);
}
我在第二个模块 test-oauth 中添加了模块 test-data 作为依赖项,如下所示:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.emo.test</groupId>
<artifactId>emo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/>
</parent>
<artifactId>test-oauth</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-oauth</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.emo.test</groupId>
<artifactId>test-data</artifactId>
<version>0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
进入模块 test-oauth,我使用实体用户:
@Transactional
@Service("userServiceImpl")
public class UserServiceImpl {
@PersistenceContext
private EntityManager entityManager;
public User findByUsername(String username) throws EmptyResultDataAccessException {
String query = "FROM Login l WHERE l.username = :username";
final Login l = (Login) entityManager.createQuery(query).setParameter("username", username).getSingleResult();
return l.getUser();
}
}
我得到了这个例外:Compilation failure: Compilation failure: cannot find symbol User, package com.test.core.entities does not exist
我的父母 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.emo.test</groupId>
<artifactId>emo-parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>test-data</module>
<module>test-oauth</module>
</modules>
<properties>
<java.version>1.11</java.version>
<oauth.version>2.2.2.RELEASE</oauth.version>
<kazi.data.version>0.1-SNAPSHOT</kazi.data.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${oauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-plugin-releases</id>
<url>https://repo.spring.io/plugins-release</url>
</pluginRepository>
</pluginRepositories>