Spring-data-jpa + Hibernate 未创建预期表
Posted
技术标签:
【中文标题】Spring-data-jpa + Hibernate 未创建预期表【英文标题】:Spring-data-jpa + Hibernate not creating expected table 【发布时间】:2017-05-29 15:58:42 【问题描述】:在您认为这是重复之前,我知道这些答案(以及其他):
Hibernate not creating Table automatically in spring boot using postgresql Unable to get Spring boot to automatically create database schema Spring Boot + Hibernate + Postgres - not creating tables不过,自动创建表格不起作用!
我使用了不同版本的Hibernate
、Spring
,甚至从JpaBaseConfiguration
实现了类JpaConfig
,并从common application properties添加了尊重属性
预期结果:
运行 hbm2ddl 架构更新
实际结果:
运行 hbm2ddl 模式导出
我看到了 org.hibernate.cfj.Configuration Iterator<Table> getTableMappings()
,但是这个方法返回的是 emty 列表而不是映射类->表
任何帮助将不胜感激。
Application.yml:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/task-manager
username: postgres
password: password
schema: public
jpa:
generate-ddl: true
hibernate:
naming-strategy: ru.ssau.common.naming_strategy.CustomNamingStrategy
ddl-auto: create-drop
logging:
level:
org:
hibernate:
SQL: DEBUG
type:
descriptor:
sql:
BasicBinder: TRACE
添加属性 driverClassName 不能解决它:
我的实体:
@Entity(name = "simple_user")
public class User extends PersistentObject
@Column(unique = true, nullable = false)
private String nickname;
@OneToOne
@JoinColumn(name = "user_account_id")
private UserAccount userAccount;
public User()
public User(String nickname)
this.nickname = nickname;
public String getNickname()
return nickname;
public void setNickname(String nickname)
this.nickname = nickname;
public UserAccount getUserAccount()
return userAccount;
public void setUserAccount(UserAccount userAccount)
this.userAccount = userAccount;
Hibernate 从控制台的输出:
HHH000412: Hibernate Core 4.3.11.Final
HHH000206: hibernate.properties not found
HHH000021: Bytecode provider name : javassist
HCANN000001: Hibernate Commons Annotations 4.0.5.Final
HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect
HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
HHH000397: Using ASTQueryTranslatorFactory
HHH000227: Running hbm2ddl schema export
HHH000230: Schema export complete
【问题讨论】:
【参考方案1】:我也面临同样的问题,但在我的情况下,我的数据库中不会自动创建各种实体之一,让我们一起尝试解决。
我的实体看起来像:
@Entity
@Table(name = "tablename")
public class ClassName
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
...
尝试像上面那样添加注释并告诉我是否有效。
Tks.
【讨论】:
问题不在于自动生成Id,而在于未创建表【参考方案2】:只需将以下内容添加到您的配置中:
spring:
jpa:
hibernate:
ddl-auto: none
properties:
hibernate.hbm2ddl.auto: create-drop
对我来说效果很好,使用 1.4.3.RELEASE。
【讨论】:
是的,我已经添加了这个属性,但是它不起作用:( 可能需要运行两次:(您之前运行的数据库结构尚未删除,因此在启动期间hibernate会尝试创建数据库结构,但是将失败,因为表已经存在;然后你停止你的应用程序并丢弃循环工作。在下次启动时应该创建结构) 尽管如此,从我的角度来看,hibernate 的 ddl-auto 例程是不好的做法。我个人在单元测试中使用它。对于生产和集成测试,我确实使用 liquibase,我强烈建议在项目中引入它。 我是这样做的 1)删除旧数据库 2)用旧名称创建新的空数据库(任务管理器)谢谢你对 liquibase 的想法。我稍后再试。以上是关于Spring-data-jpa + Hibernate 未创建预期表的主要内容,如果未能解决你的问题,请参考以下文章