@Index 不适用于 JOINED 子类?

发布于 01-05 19:06 字数 4889 浏览 3 评论 0原文

当我的应用程序在服务器上运行时,我的 Hibernate 注释起作用,但是当我尝试使用 JUnit 测试我的 Hibernate 类时,我收到以下错误:

org.hibernate.MappingException: Unable to find column with logical name base_batch_id in table T_BASE_EXT
at org.hibernate.cfg.Configuration$MappingsImpl.getPhysicalColumnName(Configuration.java:3550)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.addConstraintToColumn(IndexOrUniqueKeySecondPass.java:87)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:77)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at foo.BaseTest.setUp(BaseTest.java:31)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我相信这与 @Index 注释有关BaseExt 类。我的带注释的类正在工作并且无法更改它们,但是我可以对 JUnit 测试中的 Hibernate 配置执行某些操作才能使其正常工作吗?

我发现了一个有类似问题的票证: https://hibernate.onjira.com/browse /ANN-207

这是一个复制此错误的 JUnit 测试:

抽象基类:

@MappedSuperclass
public abstract class Base implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "SEQ", initialValue = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_GEN")
    private Long id;

    public Long getId() {
        return id;
    }

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

扩展抽象类的类:

并且它还具有 ManyToOne 关系到BaseBatch-class

@Entity
@Table(name = "T_BASE_EXT")
@org.hibernate.annotations.Table(appliesTo = "T_BASE_EXT", indexes = {
        @Index(name = "IDX_CDA_1", columnNames = {"BASE_BATCH_ID"})
})
public class BaseExt extends Base implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @ForeignKey(name = "CDA_BASE_BATCH_FK")
    private BaseBatch baseBatch;

    public BaseBatch getBaseBatch() {
        return baseBatch;
    }

    public void setBaseBatch(BaseBatch baseBatch) {
        this.baseBatch = baseBatch;
    }

}

BaseBatch:

@Entity
@Table(name = "T_BASE_BATCH")
public class BaseBatch extends Base implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

}

JUnit 测试:

public class BaseTest extends TestCase {
    private SessionFactory sessionFactory = null;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        final String driverClass = "org.hsqldb.jdbcDriver";
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Configuration cfg = new Configuration()
        .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
        .setProperty("hibernate.connection.driver_class", driverClass)
        .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:basetest")
        .setProperty("hibernate.hbm2ddl.auto", "create");

        cfg.addAnnotatedClass(Base.class);
        cfg.addAnnotatedClass(BaseExt.class);
        cfg.addAnnotatedClass(BaseBatch.class);

        sessionFactory = cfg.buildSessionFactory();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        sessionFactory.close();
    }

    public void testBase() {
        sessionFactory.openSession();
        assertTrue(true);
    }
}

我正在使用 Hibernate Annotations 3.2.0.Final 和 Hibernate 3.6.9.Final

My Hibernate annotations work when my application is running on the server, but when I try to test my Hibernate classes with JUnit I get the following error:

org.hibernate.MappingException: Unable to find column with logical name base_batch_id in table T_BASE_EXT
at org.hibernate.cfg.Configuration$MappingsImpl.getPhysicalColumnName(Configuration.java:3550)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.addConstraintToColumn(IndexOrUniqueKeySecondPass.java:87)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:77)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at foo.BaseTest.setUp(BaseTest.java:31)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I believe this has something to do with the @Index annotation at BaseExt class. My annotated classess are working and can't change them, but is there something that I can do to my Hibernate configuration in the JUnit test to get this working?

I've found a ticket that had a similar issue: https://hibernate.onjira.com/browse/ANN-207

Here is a JUnit test to replicate this error:

Abstract base class:

@MappedSuperclass
public abstract class Base implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "SEQ", initialValue = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_GEN")
    private Long id;

    public Long getId() {
        return id;
    }

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

Class that extends the abstract class:

And it also has a ManyToOne relationship to BaseBatch-class

@Entity
@Table(name = "T_BASE_EXT")
@org.hibernate.annotations.Table(appliesTo = "T_BASE_EXT", indexes = {
        @Index(name = "IDX_CDA_1", columnNames = {"BASE_BATCH_ID"})
})
public class BaseExt extends Base implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @ForeignKey(name = "CDA_BASE_BATCH_FK")
    private BaseBatch baseBatch;

    public BaseBatch getBaseBatch() {
        return baseBatch;
    }

    public void setBaseBatch(BaseBatch baseBatch) {
        this.baseBatch = baseBatch;
    }

}

BaseBatch:

@Entity
@Table(name = "T_BASE_BATCH")
public class BaseBatch extends Base implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

}

The JUnit test:

public class BaseTest extends TestCase {
    private SessionFactory sessionFactory = null;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        final String driverClass = "org.hsqldb.jdbcDriver";
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Configuration cfg = new Configuration()
        .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
        .setProperty("hibernate.connection.driver_class", driverClass)
        .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:basetest")
        .setProperty("hibernate.hbm2ddl.auto", "create");

        cfg.addAnnotatedClass(Base.class);
        cfg.addAnnotatedClass(BaseExt.class);
        cfg.addAnnotatedClass(BaseBatch.class);

        sessionFactory = cfg.buildSessionFactory();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        sessionFactory.close();
    }

    public void testBase() {
        sessionFactory.openSession();
        assertTrue(true);
    }
}

I'm using Hibernate Annotations 3.2.0.Final and Hibernate 3.6.9.Final

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名2025-01-12 19:06:36

添加此注释

@AttributeOverride(name="id", column=@Column(name="base_batch_id"))
private BaseBatch baseBatch;

Add this annotation on

@AttributeOverride(name="id", column=@Column(name="base_batch_id"))
private BaseBatch baseBatch;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文