If Java Annotation doesn't have business logic on it, then how does @Entity in JavaEE could map a class in database?

huangapple go评论75阅读模式
英文:

If Java Annotation doesn't have business logic on it, then how does @Entity in JavaEE could map a class in database?

问题

我在某处看到过,注解只是元数据,不包含任何业务逻辑。

因此,我查看了 @Entity 注解的代码:

包 javax.persistence;
导入 java.lang.annotation.Documented;
导入 java.lang.annotation.ElementType;
导入 java.lang.annotation.Retention;
导入 java.lang.annotation.RetentionPolicy;
导入 java.lang.annotation.Target;

@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
    String name() 默认值 """;
}

现在,这段代码如何在没有在其上定义方法的情况下将类映射到数据库中呢?

英文:

I read somewhere that annotations are only metadata and do not contain any business logic.

So I look at the code of @Entity annotation:

package javax.persistence;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
    String name() default "";
}

Now, how this code could map a class to a database without a method define on it?

答案1

得分: 2

@Entity本身没有逻辑,只是一个规范。
业务逻辑是由第三方代码实现的。

例如,如果您正在使用Spring Boot,您可以查看类@EntityScan,以了解它如何处理@Entity类。

英文:

@Entity itself has no logic but a specification.
The business logic is implement by third party code.

For example, if you are using Spring Boot, you can follow the class @EntityScan to see how it handle @Entity classes.

huangapple
  • 本文由 发表于 2020年10月16日 13:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定