Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot Entity Scanning

DZone's Guide to

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.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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? 

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
java ,spring framework ,spring boot ,entities ,tutorial

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}