0

I created a multiple module spring boot project with a parent pom and 3 submodules.

The module test-data and test-service doesn't have a main class. I used it as jar. In the module test-data, i added some Entities-Classes User and Login:

@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

The User class:

@Table
@Entity
public class User implements Serializable{
...
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(referencedColumnName = "login_id")
    private Login login;
...

The pom.xml of module test-data:

 <parent>
        <groupId>com.emo.test</groupId>
        <artifactId>emo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <artifactId>test-data</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.8.2</version>
        </dependency>
    </dependencies>

In the module test-service, i added some some service-classes:

package com.test.api.serviceImpl;

import com.test.core.entities.Login;
import com.test.core.entities.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@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();
    }
}

The pom of the test-service:

<parent>
        <groupId>com.emo.test</groupId>
        <artifactId>test</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>

    <artifactId>test-service</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <name>test-service</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>com.test.kazi</groupId>
            <artifactId>test-data</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

In my test-oauth i have a main class, i want to run this module:

The main class of 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);
    }

I added the module test-service in the module test-oauth as dependency like this:

<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>${parent.version}</version>
    <packaging>jar</packaging>

    <name>test-oauth</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>com.emo.test</groupId>
            <artifactId>test-service</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

Into the module test-oauth, i use the entity UserServiceImpl:

package com.oauth.server.security;

import com.test.api.serviceImpl.UserServiceImpl;
import com.test.core.entities.Login;
import com.test.core.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    private UserServiceImpl userServiceImpl;

    @Autowired
    public CustomUserDetailsService(UserServiceImpl userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        final User user = userServiceImpl.findByUsername(username);
        if (user == null)
            throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
        return new UserRepositoryUserDetails(user);
    }

I got this exception:

{"error":"unauthorized","error_description":"org.hibernate.hql.internal.ast.QuerySyntaxException: Login is not mapped [FROM Login l WHERE l.username = :username]"}* Closing connection 0

My Parent pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.emo.test</groupId>
    <artifactId>test</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>test-data</module>
        <module>test-service</module>
        <module>test-oauth</module>
    </modules>

    <properties>
        <java.version>1.11</java.version>
        <oauth.version>2.2.2.RELEASE</oauth.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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <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>
emoleumassi
  • 4,881
  • 13
  • 67
  • 93

1 Answers1

2

The issue is that your entity classes are not recognized as JPA entities. Springboot does that automagically when the entities are declared in the same package or sub package of your main entry point. Since your entry point belongs to package com.oauth.server and your entities are in package com.test.core.entities you have to configure it "manually" by using @EntityScan.

Example done in your main entry point:

@SpringBootApplication
@EntityScan(basePackages = "com.test.core.entities")
public class OAuthApplication {
    /* ... */
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • now i have this error: org.hibernate.exception.SQLGrammarException: could not extract ResultSet – emoleumassi May 10 '20 at 17:26
  • @emoleumassi please provide more info of the exception. – Luiggi Mendoza May 10 '20 at 17:52
  • i added the `@EntityScan` to `OAuthApplication` with the same code i got now an error `org.hibernate.exception.SQLGrammarException: could not extract ResultSet` – emoleumassi May 10 '20 at 18:36
  • 1
    That usually happens when you need to tune your relationships between entities. Please see https://stackoverflow.com/q/20089031/1065197 – Luiggi Mendoza May 10 '20 at 19:35
  • There a `OneToOne` relationship between both table. `username` has a unique constraints. In my database i have just one element with this `username` – emoleumassi May 11 '20 at 05:15