首页 > 解决方案 > 如何在hibernate中使用类org.hibernate.annotations.Table的@Table注解

问题描述

我的班级名称是

批处理作业

我的表名是

批处理作业

我正在使用 org.hibernate.annotations.Table 类的 @Table 而不是 javax.persistence

我的 BatchJob 类代码是

import javax.persistence.Entity;
import org.hibernate.annotations.Table;


@Entity
@Table(appliesTo="batch_jobs")
public class BatchJob {

我收到错误

org.hibernate.AnnotationException:@org.hibernate.annotations.Table 引用了一个未知的表:batch_jobs

当我将@Entity 表示法的类更改为 org.hibernate.annotations

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;


@Entity
@Table(appliesTo="batch_jobs")
public class BatchJob {

我收到错误

org.hibernate.hql.internal.ast.QuerySyntaxException:BatchJob 未映射 [来自 BatchJob]

我的查询

allBatchJobs=session.createQuery("From BatchJob").list();

我的 hibernate.cfg.xml 映射

<mapping class="com.company.bmdashboard.beans.BatchJob"></mapping>

当我将我的类名更改为 batch_jobs 时,代码工作正常,但我想使用名称 BatchJob 并且我不想使用 javax.persistence 类。

请指导我。提前致谢

标签: javaxmlspringhibernateannotations

解决方案


@org.hibernate.annotations.Table使用来自javax.persistence.Table;和/或来自的值javax.persistence.SecondaryTables;。例如:

@Table(name = "batch_jobs")
@SecondaryTables({
        @SecondaryTable(name="batch")
})
@Entity
@org.hibernate.annotations.Table(inverse = true, appliesTo = "batch")
public class BatchJob {

所以,@org.hibernate.annotations.Table不等于javax.persistence.Table;,这是不同目的的不同注解。


推荐阅读