Reputation: 11649
In my spring app i have the below entity:
import lombok.Data;
@Data
@Entity
public class Profile {
@Id
@GeneratedValue
Long id;
String name;
String address;
String description;
String img;
public Profile(String name, String address, String description, String img) {
this.name = name;
this.address = address;
this.description = description;
this.img = img;
}
}
and then the repository is:
@Repository
public interface ProfileRepository extends JpaRepository<Profile,Long> {
}
Here is my pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
So, i expect when i go to the h2 console uri:
http://localhost:8080/h2-console/login.do?jsessionid=ae6ba7021a23e0ebb4a4844381546e72
It shows the table related to the object Profile
.
The problem is: there is no table called Profile.
So, why i don't have the profile table?
The application.properties
is empty
And when i run my application, it says:
[2m2017-06-29 10:07:21.106[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36morg.hibernate.dialect.Dialect [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
[2m2017-06-29 10:07:21.557[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaExport [0;39m [2m:[0;39m HHH000227: Running hbm2ddl schema export
[2m2017-06-29 10:07:21.557[0;39m [32m INFO[0;39m [35m51392[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaExport [0;39m [2m:[0;39m HHH000230: Schema export complete
So, it seems that, the application makes the schema of database. Am i right?
Upvotes: 1
Views: 2218
Reputation: 26
I had the same problem, I solved it by moving the entity to the main app package.
Note: using @ComponentScan over the main app class didn't solve the issue nither a separate class with @Configuratin @ComponentScan ( basePackages = {"thepackage"})
Upvotes: 0
Reputation: 21452
you need to set h2 configuration in application.properties
write these lines
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb --> whatever url you find when open console
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
Upvotes: 1
Reputation: 30429
Then you open 'http://localhost:8080/h2-console' you need to specify 'JDBC URL'. By default (as I understand you use default setting because your application.properties is empty) this value is:
jdbc:h2:mem:testdb
IMHO H2 Console is not the best choice for the developer. You can work with H2 right from your IDE - see my answer.
Upvotes: 0
Reputation: 166
You can also achieve so by writing the schema.sql file that creates tables and can load data by placing the file in your src/main/resources location. There are a lot of different ways you can do this. I like the schema.sql file because it has something that you can look at and manage.
Upvotes: 0
Reputation: 41
Check if your main class that contains the annotation @SpringBootConfiguration is in the root of hierarchy of your package
Upvotes: 0