DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • How To Build Web Service Using Spring Boot 2.x
  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Should Developers Switch From SQLite to DuckDB?
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot Entity Scanning

Spring Boot Entity Scanning

If your packages aren't in the right place, you're going to have a bad time. See how to tell Spring where your entities are so they don't get lost in the fray.

By 
Dan Vega
·
Apr. 05, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
117.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

I had a question from a student come up that a lot of new Spring developers come across. It has to do with creating components and, in this case more specifically, entities. Let's take a look at the question and then dive into the solution. 

The Problem

Hey, I'm trying to get JPA to create the post and author tables and view them in the H2 console. I copied every step in the video and tried the suggestions to change the JDBC URL to :testdb. I've also tried changing spring.datasource.name: to both test and testdb, all with no luck. When the console comes up, there are no tables. Any help would be most appreciated!

The Solution

I had to dive into the student's code to figure out what was going on, but there is usually a pretty good explanation as to why something like this is happening. When entities aren't being picked up and database tables aren't being created, there are a few places to look.

The first thing I do is make sure we are connecting to the correct database, and in this case, we were. Next, I like to take a look at the entities and make sure there are no issues with those. We could have issues with the code, or it simply could be an issue with Spring not finding the entity based on its location. 

Create a New Spring Boot Project

To demonstrate the issue at hand, we are going to create a brand new Spring Boot project. This will give us a chance to see what the problem is in a simplified version. Create a new project using the Spring Initializr and select the Web, JPA, and H2 dependencies. 

When you are creating the project, you will be asked to give your main package a name. I named mine com.therealdanvega, and as you can see from the image below, it created the main application class under that package. 

Create an Entity

In the project, I asked the student to create a package named domain and create a new entity there. The student did just that, and here is what that class look likes. 

package domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Tweet {

    @Id @GeneratedValue
    private Long id;
    private String username;
    private String text;

    private Tweet() {}

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}


If you know what you're looking for, you can probably see the issue on line 1 of that code. The student created the domain package under the Java folder and not inside of the main com.therealdanvega package. The student started the application and the table wasn't being created. As we will see, this is expected behavior. 

@EntityScan Annotation

The easy fix is to move the package and class under the main package, and this will work as expected. Another way we can solve this problem is by telling Spring where to find our entities. We can do so by using the @EntityScan annotation. We can use the base packages attribute and point to one or many packages to include when scanning for entities. We will add this annotation to our main application class. 

package com.therealdanvega;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan( basePackages = {"domain"} )
public class EntityDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EntityDemoApplication.class, args);
    }
}


Now if you start up the application and go to your H2 console, you should see that the tweet table was created for you. 

As you can see, this is a pretty easy mistake to make. If you would like to check out this project, you can find the source code for it on Github. I hope you found this tutorial helpful, and with that, I will leave you with a question. 

Question: What is a common mistake you made in your first few Spring Boot projects? 

Spring Framework Spring Boot Database

Published at DZone with permission of Dan Vega. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • How To Build Web Service Using Spring Boot 2.x
  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook