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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • A Complete Guide to Modern AI Developer Tools
  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 user avatar
Dan Vega
·
Apr. 05, 17 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
114.9K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!