You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仅用Hibernate注解注册实体并生成表时遇ORA-00942错误求助

问题

使用Hibernate 5.6.15,通过addAnnotatedClass(Class annotatedClass)注册实体,调用Session保存实体时触发"ORA-00942: 表或视图不存在"错误,且数据库表未自动创建。已配置hibernate.ddl-auto=create,尝试提前开启并关闭Session、事务仍无效,因兼容Java 8需使用Hibernate 5版本。

主类代码

import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import ua.gov.ioc.exchange.config.HibernateConfig;
import ua.gov.ioc.exchange.obituary.model.TestEntity;

public class HibernateApp {
    public static void main(String[] args) {
        SessionFactory sessionFactory = buildSessionFactory();

        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
        TestEntity entity = new TestEntity();
        session.save(entity);

        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    private static SessionFactory buildSessionFactory() {
        try {
            AnnotationConfiguration configuration = new AnnotationConfiguration();
            Properties properties = PropertiesReader.get();
            configuration.setProperties(properties);
            configuration.addAnnotatedClass(app.model.TestEntity.class);
            configuration.buildMappings();
            return configuration.buildSessionFactory();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
}

实体类代码

import javax.persistence.*;
import org.hibernate.annotations.BatchSize;

@Entity
@Table(name = "TestEntity")
public class TestEntity {
    @Id
    private Long id = 1L;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

配置属性

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.ddl-auto=update
hibernate.connection.url=jdbc:oracle:thin:@10.0.0.181:1521:center
hibernate.connection.username=username
hibernate.connection.password=password
hibernate.connection.autocommit=true

解决方案

1. 替换过时的AnnotationConfiguration

Hibernate 5开始,AnnotationConfiguration已被废弃,推荐使用org.hibernate.cfg.Configuration,该类已整合注解支持,旧类可能导致配置加载不完整。

2. 修正实体类路径

主类中addAnnotatedClass传入的app.model.TestEntity.class与导入的ua.gov.ioc.exchange.obituary.model.TestEntity路径不匹配,导致实体未被正确注册,需统一路径。

3. 统一ddl-auto配置

问题描述中设置的是create,但配置文件里写的是update,需保持一致。优先使用create确认表能生成,后续再根据业务需求调整为update或validate。

4. 关闭自动提交

开启hibernate.connection.autocommit=true会与手动事务管理冲突,导致DDL语句无法正常执行,需设置为false,由Hibernate管理事务。

5. 匹配Oracle版本方言

若使用Oracle 12c及以上版本,建议替换为org.hibernate.dialect.Oracle12cDialect,确保DDL语法符合数据库版本要求。

修改后的代码示例

重构buildSessionFactory方法

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        Properties properties = PropertiesReader.get();
        configuration.setProperties(properties);
        // 修正实体类路径,与导入包一致
        configuration.addAnnotatedClass(ua.gov.ioc.exchange.obituary.model.TestEntity.class);
        return configuration.buildSessionFactory();
    } catch (Exception e) {
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }
}

调整配置属性

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
# 根据Oracle版本调整方言
hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
# 统一设置为create,确保表被创建
hibernate.ddl-auto=create
hibernate.connection.url=jdbc:oracle:thin:@10.0.0.181:1521:center
hibernate.connection.username=username
hibernate.connection.password=password
# 关闭自动提交,保证事务正常执行
hibernate.connection.autocommit=false

内容的提问来源于stack exchange,提问作者user9999

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 19:24:56