Reputation: 109
I am working on a spring boot application that uses H2, JPA, and maven. I want spring boot to create my tables automatically. But If I dont want to put my entity classes where I put my core classes. If I do not put my entity classes in the same or subpackage of my core package where I have my @SpringBootApplication notation then the spring does not create my tables. I wonder is there a way to work this around? I want to put my entity classes into different package than my @SpringBootApplication class and still make spring to create my tables in H2. Please see below. As you see my entity class is in com.dao and my runner class is in com.core.
package com.dao;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "method")
public class Method {
@Id
private long id;
private String name;
// Setters getters
}
package com.core;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Runner {
public static void main(String[] args) {
SpringApplication.run(Runner.class, args);
System.out.println("Boot");
}
}
Upvotes: 1
Views: 5231
Reputation: 1118
How I ended up creating tables in the project is using data.sql and schema.sql
You can add one data.sql where you can put your table creation
CREATE TABLE IF NOT EXISTS `springsecurity1`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(120) NOT NULL,
`enabled` INT NOT NULL,
`password` VARCHAR(120) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf32
COLLATE = utf32_bin;
TRUNCATE table springsecurity1.users;
insert into springsecurity1.users values (NULL, 'yavdhesh',1,'12345');
After you start you spring boot application. You can find it in database as well.
Hope it is useful to someone.
Upvotes: 1
Reputation: 11
Traditionally, JPA “Entity” classes are specified in a persistence.xml file. With Spring Boot, this file is not necessary and “Entity Scanning” is used instead. By default, all packages below your main configuration class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication) are searched.
This translates to com.core.* will be scanned. The problem you're experiencing is a result of using com.dao instead of com.core.*. There is by no means a restriction that Spring Boot has to be in one package, and it is usually assumed that the Application Main simply exists at the root level.
Upvotes: 1
Reputation: 840
Use this @ComponentScan(basePackages = "app, entities, repository, services")
on the spring boot app configuration class, to actually tell spring where to scan for components and @Entity
classes, if they are not in sub-packages of the same package your configuration files is in.
in this example, we have different packages containing different kinds of classes. package names are: app, entities, repository, services, or whatever you already have or want to name them..
Goodluck!
Upvotes: 0