Hibernate Annotation relations does not work with spring.jpa.generate-ddl=true

走远了吗. 提交于 2021-02-08 08:55:23

问题


I am creating some simple Spring Boot project with Hibernate JPA. I created some data model which consists 5 tables for now and created entities reflecting tables. I have set spring.jpa.generate-ddl=true and my entities was correctly reflected by schema created in PostgreSQL.

Next step was to start adding relations.

Part of my assumed datamodel is (paron my UML)

Very simple one to many relation.

My entities look that way (getters and setters omitted below, exist in code):

@Entity
public class AppUser {

  @Id
  @GeneratedValue
  private long id;

  private String name;
  private String secondName;
  private String email;
  private java.util.Date joinDate;

  @ManyToOne
  @JoinColumn(name = "user_role_id")
  private UserRole userRole;
}


@Entity
public class UserRole {

  @Id
  @GeneratedValue
  private long id;

  private String roleName;
}

I launch my application with spring.jpa.generate-ddl=true and column user_role_id gets created in AppUser table but application fails to start due errors:

2018-10-11 19:41:35.435  INFO 45564 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2018-10-11 19:41:35.466  WARN 45564 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42703
2018-10-11 19:41:35.466 ERROR 45564 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: column t1.tgconstrname does not exist

There is full stacktrace (please advise if should paste it here instead of pastebin: https://pastebin.com/x4qNJkK9

When I set spring.jpa.generate-ddl=false application starts succesfully.

Any ideas why is that happening?

来源:https://stackoverflow.com/questions/52766163/hibernate-annotation-relations-does-not-work-with-spring-jpa-generate-ddl-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!