问题
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