使用JPA的Hibernate不会创建表

时间:2017-06-24 22:03:31

标签: java hibernate maven jpa spring-boot

我很沮丧,Spring boot-hibernate不是为最简单的程序自动创建表。

@Entity
@Table(name = "Question")
public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    String id;

    String name;

    // Default constructor

    // Parametrized constrcutor

    // Getters-Setters
}

主档......

@SpringBootApplication
public class DbTestApplication {

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

Application.properties

# Datasource
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test
spring.datasource.username=scott
spring.datasource.password=tiger
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

控制台:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-06-24 23:58:05.497  INFO 4842 --- [           main] org.example.hotel.DbTestApplication      : Starting DbTestApplication on PC192-168-2-107 with PID 4842 (/Users/bhalchandra/Documents/workspace-sts-3.8.3.RELEASE/DbTest/target/classes started by bhalchandra in /Users/bhalchandra/Documents/workspace-sts-3.8.3.RELEASE/DbTest)
2017-06-24 23:58:05.504  INFO 4842 --- [           main] org.example.hotel.DbTestApplication      : No active profile set, falling back to default profiles: default
2017-06-24 23:58:05.571  INFO 4842 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f490758: startup date [Sat Jun 24 23:58:05 CEST 2017]; root of context hierarchy
2017-06-24 23:58:06.755  INFO 4842 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-24 23:58:06.778  INFO 4842 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-06-24 23:58:06.862  INFO 4842 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-06-24 23:58:06.864  INFO 4842 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-06-24 23:58:06.866  INFO 4842 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-06-24 23:58:06.914  INFO 4842 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-06-24 23:58:07.071  INFO 4842 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-06-24 23:58:07.446  INFO 4842 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-06-24 23:58:07.448  INFO 4842 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@53b98ff6
2017-06-24 23:58:07.922  INFO 4842 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-06-24 23:58:07.936  INFO 4842 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2017-06-24 23:58:07.996  INFO 4842 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-24 23:58:08.364  INFO 4842 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-24 23:58:08.380  INFO 4842 --- [           main] org.example.hotel.DbTestApplication      : Started DbTestApplication in 3.252 seconds (JVM running for 3.632)
2017-06-24 23:58:08.381  INFO 4842 --- [       Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f490758: startup date [Sat Jun 24 23:58:05 CEST 2017]; root of context hierarchy
2017-06-24 23:58:08.382  INFO 4842 --- [       Thread-3] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-06-24 23:58:08.383  INFO 4842 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-06-24 23:58:08.383  INFO 4842 --- [       Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-06-24 23:58:08.386  INFO 4842 --- [       Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

的pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1201-jdbc41</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
</dependency>

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>

这是我尝试使用注释创建表的最简单的程序。最后,在数据库中没有创建表。有谁可以帮帮我?对我来说似乎一切。

2 个答案:

答案 0 :(得分:0)

好像你需要为Spring启用EntityScan才能找到你的模型类:

@SpringBootApplication
@EntityScan // <-- find all classes annotated with @Entity
public class DbTestApplication {

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

答案 1 :(得分:0)

我发现了自己的错误 package org.java.domain实体位于DbTestApplicationpackage org.java.dbtest位于<?xml version="1.0" encoding="utf-8"?> <workbook> <worksheet name="Sheet1"> <section startRow="0" endRow="0"> </section> <loop startRow="1" endRow="1" items="rows" var="row" varType="java.util.HashMap"> <section startRow="1" endRow="1"> <mapping row="1" col="0" type="java.lang.String">row.id</mapping> <mapping row="1" col="1" type="java.lang.String">row.name</mapping> <mapping row="1" col="2" type="java.lang.String">row.code</mapping> </section> <loopbreakcondition> <rowcheck offset="0"> <cellcheck offset="0">Summary</cellcheck> </rowcheck> </loopbreakcondition> </loop> </worksheet> </workbook>

对于spring boot,它们必须位于同一个包或子包中。