【问题标题】:what am i missing for using hibernate annotation?使用休眠注释我缺少什么?
【发布时间】:2011-08-08 20:10:20
【问题描述】:

我正在尝试使用最新的 hibernate 创建一个基本的 hibernate 实体 POJO,并且我已经添加了我从 hibernate 网站下载的必要 jar 文件。

问题是当我添加行@Table(name = "user")

它抱怨编译错误:

注解@Table 必须定义 属性applyTo

完整代码如下:

package com.jr.entities.users;

import java.io.Serializable;

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

@Entity
@Table(name = "user")
public class DAOuser implements Serializable{
    
    private String uid;
    private String emailAddress;
    private String username;
    private String password;
    

}

在此示例链接http://www.roseindia.net/hibernate/hibernateannotations/hibernate-annotations-tutorial.shtml 中,它表示不需要设置 applyTo 值?我错过了什么吗?如果有帮助,我在 eclipse J2ee 中创建了一个简单的 EJB3 项目。

提前致谢

【问题讨论】:

    标签: hibernate ejb-3.0


    【解决方案1】:

    有两组持久化注解(@Entity@Table)——JPA 注解(在javax.persistence 包中)和Hibernate 注解(在org.hibernate.annotations 包中)。请注意,示例使用 JPA 注释,而您的代码使用 Hibernate 注释,因此您的代码无法编译,因为这些注释具有不同的属性集。

    因此,您需要更改 import 语句中的包。

    通常你应该使用 JPA 注释,除非你需要一些只有 Hibernate 注释提供的特性。

    【讨论】:

    • 嗯,好的。有没有关于如何在纯休眠注释中执行上述操作的示例?我可以将hibernate与jpa注释混合使用吗?即为我的实体使用 jpa 注释并使用 Hibernate.cfg.xml 配置数据源?
    • @jonney:见docs.jboss.org/hibernate/annotations/3.5/reference/en/html。您可以根据需要混合使用 Hibernate 和 JPA 注释。
    【解决方案2】:

    appliesTo 是包含目标表的名称:

    @Table(appliesTo="user")
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2015-08-31
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多