详解 @Entity 和 @Table 注解的用法

最近看了很多文章都说,@Entity 和 @Table 注解是 Hibernate 中的注解,甚至一些文章的标题都直接这样写。我都不知道他们这样误导了多少人,今天我就来给大家纠正一下!

@Entity 注解和 @Table 注解都是 Java Persistence API 中定义的一种注解。你说它是 jpa、hibernate、Spring 等中的注解都不太准确。@Entity 和 @Table 注解都必须遵循 Java Persistence API 中定义的一种查询语言(JPQL)。

@Entity 和 @Table 是 JDK1.5 以后支持的元数据注解(Annotation)。

@Entity 注解源码如下:

1

2

3

4

5

6

7

package javax.persistence;

@java.lang.annotation.Documented

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)

public @interface Entity {

    java.lang.String name() default "";

}

@Table 注解源码如下:

1

2

3

4

5

6

7

8

9

10

package javax.persistence;

@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)

public @interface Table {

    java.lang.String name() default "";

    java.lang.String catalog() default "";

    java.lang.String schema() default "";

    javax.persistence.UniqueConstraint[] uniqueConstraints() default {};

    javax.persistence.Index[] indexes() default {};

}

@Entity 说明这个 class 是实体类,并且使用默认的 orm 规则,即 class 名即数据库表中表名,class 字段名即表中的字段名。@Entity 注解指名这是一个实体 Bean。下面我们看一个例子:

1

2

3

4

5

6

7

8

9

10

@Entity

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id

    private Long id;

    private String name;

    private int age;

    private String addree;

   // Getters and Setters

}

如果没有 @javax.persistence.Entity 和 @javax.persistence.Id 这两个注解的话,它完全就是一个典型的 POJO 的 Java 类,现在加上这两个注解之后,就可以作为一个实体类与数据库中的表相对应。他在数据库中的对应的表为:

@Entity 注解 ER 图

对应的映射规则为:

  • 实体类必须用 @javax.persistence.Entity 进行注解;如果不用这个注解会报 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne 异常

  • 必须使用 @javax.persistence.Id 来注解一个主键;如果没有这个注解会报:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity 异常

  • 实体类必须拥有一个 public 或者 protected 的无参构造函数,之外实体类还可以拥有其他的构造函数;如果没有无参构造函数会报 org.springframework.orm.jpa.JpaSystemException: No default constructor for entity:  : com.xttblog.entity.XttblogTest; nested exception is org.hibernate.InstantiationException: No default constructor for entity:  : com.xttblog.entity.XttblogTest 异常

  • 实体类必须是一个顶级类(top-level class)。一个枚举(enum)或者一个接口(interface)不能被注解为一个实体;

  • 实体类不能是 final 类型的,也不能有 final 类型的方法;

  • 如果实体类的一个实例需要用传值的方式调用(例如,远程调用),则这个实体类必须实现(implements)java.io.Serializable 接口。

@Table 注解是一个非必须的注解。@Table 注解指定了 Entity 所要映射带数据库表,其中 @Table.name() 用来指定映射表的表名。声明此对象映射到数据库的数据表,通过它可以为实体指定表(talbe),目录 (Catalog) 和 schema 的名字。

如果同时使用了 @Entity(name="student") 和 @Table(name="students"),最终的对应的表名必须是哪个?答案是 students,这说明优先级:@Table > @Entity。

https://www.xttblog.com/?p=3660

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值