Hibernate + JPA:模式验证:缺少列

Posted

技术标签:

【中文标题】Hibernate + JPA:模式验证:缺少列【英文标题】:Hibernate + JPA: Schema-validation: missing column 【发布时间】:2018-10-19 00:22:27 【问题描述】:

我写了一个教育项目。这是一个简单的聊天。后端技术栈是Java、Jetty(服务器、websocket)、Hibernate 5.3.7.Final、PostgreSQL。

我有一个运行时异常org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [conversation_c_id] in table [conversation_reply]。这个错误让我在几天内玩得很开心。我将不胜感激帮助解决这个问题。

这是实体部分的基本结构:

@Entity
@Table(name = "conversation") //public class ConversationDataSet

@Id
@SequenceGenerator(name = "cIdSequence", sequenceName = "c_id_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cIdSequence")
@Column(name = "c_id", nullable = false, updatable = false)
private long cId;

@OneToMany(mappedBy = "conversation", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ConversationReplyDataSet> conversationReplies = new ArrayList<>();



@Entity
@Table(name = "conversation_reply") //public class ConversationReplyDataSet

@Id
@SequenceGenerator(name = "crIdSequence", sequenceName = "cr_id_sequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "crIdSequence")
@Column(name = "cr_id", nullable = false, updatable = false)
private long crId;

@Column(name = "c_id_fk")
private long cIdFk;

@ManyToOne(fetch = FetchType.LAZY)
private ConversationDataSet conversation;

SQL 架构是:

create table conversation
(
    c_id bigint not null primary key
);

create table conversation_reply
(
    cr_id bigint not null primary key,
    c_id_fk bigint not null,
    constraint fk_c_id_fk foreign key (c_id_fk) references conversation(c_id)
);

堆栈跟踪:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [conversation_c_id] in table [conversation_reply]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateTable(AbstractSchemaValidator.java:136)
at org.hibernate.tool.schema.internal.GroupedSchemaValidatorImpl.validateTables(GroupedSchemaValidatorImpl.java:42)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:89)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:191)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at dbService.DBServiceImpl.createSessionFactory(DBServiceImpl.java:155)
at dbService.DBServiceImpl.<init>(DBServiceImpl.java:42)
at main.Main.main(Main.java:23)

以防万一 Hibernate 配置为:

configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL94Dialect");
configuration.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
configuration.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/chatroomdb");
configuration.setProperty("hibernate.connection.username", "someusername");
configuration.setProperty("hibernate.connection.password", "somepassword");
configuration.setProperty("hibernate.show_sql", "true");
configuration.setProperty("hibernate.hbm2ddl.auto", "validate");
//   configuration.setProperty("hibernate.implicit_naming_strategy", "legacy-jpa");

谢谢。

【问题讨论】:

【参考方案1】:

将您的 ManyToOne 关系更改为:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_id_fk")
private ConversationDataSet conversation;

并删除它

@Column(name = "c_id_fk")
private long cIdFk;

你可以在这里阅读更多 https://en.wikibooks.org/wiki/Java_Persistence/ManyToOne#Example_of_a_ManyToOne_relationship_annotations

【讨论】:

礼萨,谢谢你的回答。我建议它不会工作。我认为专栏不能引用它自己的实体。不过我今晚会试试看。 Reza,它的工作!你能解释一下发生了什么吗?为什么我们必须从同一个实体中引用实体的列? Hibernate 在执行这个决定时收到了什么样的指令?这很有趣。对不起我过早的怀疑。非常感谢您的帮助。 你可以在这里阅读更多en.wikibooks.org/wiki/Java_Persistence/… 哦,是的……我读了好几遍……谢谢你帮助我摆脱昏迷。

以上是关于Hibernate + JPA:模式验证:缺少列的主要内容,如果未能解决你的问题,请参考以下文章

模式验证:缺少表 [hibernate_sequences]

模式验证:使用 Hibernate 序列生成器策略时缺少表 [SEQUENCE_NAME]

Spring Boot 2.1 缺少多个 org.hibernate.jpa.event 类

JPA + Spring 4 + Hibernate 4:用户缺少权限或找不到对象:USER_GROUP

Hibernate 默认模式和表注释

Flyway Spring JPA2 集成 - 可以保持模式验证吗?