【问题标题】:how to use @Table annotation of class org.hibernate.annotations.Table in hibernate如何在hibernate中使用类org.hibernate.annotations.Table的@Table注解
【发布时间】:2019-03-19 18:36:16
【问题描述】:

我的班级名称是

批量作业

我的表名是

批处理作业

我正在使用 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 类。

请指导我。提前致谢

【问题讨论】:

  • 您使用的是哪个版本的 Hibernate?可能与以下问题有关HHH-10913
  • 我正在使用 4.3.11 。我阅读了您提供的链接,但没有给我任何解决方案。你有什么想法吗?
  • 从那个问题看来,在我看来,您的要求是不可行的,因为 applyTo 属性必须引用 已经存在 表,它不打算声明名称像你一样的桌子。

标签: java xml spring hibernate annotations


【解决方案1】:

@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;,这是不同用途的不同注解。

【讨论】:

    【解决方案2】:

    请使用下面的 javax.persistence 包

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    

    【讨论】:

    • 我不想为@Table 使用 javax.persistence。
    【解决方案3】:

    试试这个....

    import javax.persistence.Entity;
    import org.hibernate.annotations.Table;
    
    @Entity
    @Table(name="batch_jobs")
    public class BatchJob {
    

    【讨论】:

    • @Table 没有 (name=" " ) 功能
    猜你喜欢
    • 2013-05-22
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2020-05-05
    • 2013-09-30
    • 2018-12-24
    相关资源
    最近更新 更多