Hibernate嵌入式对象无需注解。

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

Hibernate embeddable without annotation

问题

我正在尝试开发一个带有通用类的库,以便集成到其他项目中。例如,一个表示区间的类:

@Data
public class Interval {
    private Instant start;
    private Instant end;
}

由于这个库可能会被那些不使用 Hibernate / Spring 的项目使用,我希望不需要包含 Hibernate 依赖及其注解。

从与 Hibernate 一起使用此库的项目的角度来看,是否有办法使上述类在不创建子类和自己注解子类的情况下成为 Embeddable

英文:

I'm trying to develop a library with generic classes to integrate into other projects. For example, a class representing interval:

@Data
public class Interval {
    private Instant start;
    private Instant end;
}

Since this library might be used by the projects that do not work with Hibernate / Spring, i would like not to include Hibernate dependency and its annotations.

Is there any way, from the perspective of a project using this library together with Hibernate, to make the class above Embeddable without subclassing it and annotating the child class myself ?

答案1

得分: 1

好的,以下是翻译好的内容:

首先,注解只是注解,所以依赖于这些注解的项目并不需要在使用这些类时依赖于 JPA/Hibernate。如果注解类不可用,那么这些注解将不会可用。

除此之外,你还可以创建一个 orm.xml 文件来提供映射信息。你的库的用户将需要导入该文件。

英文:

Well, for once, annotations are just annotations, so depending projects don't need a dependency on JPA/Hibernate to use the classes. The annotations will simply not be available if the annotation classes are not available.

Apart from that, you could also create an orm.xml file that provides the mapping information. Users of your library would then have to import that file.

答案2

得分: 0

供日后参考,使用Hibernate创建自定义类型将是一个明智的选择,可以将外部库集成到受Hibernate管理的实体中。

链接:https://www.baeldung.com/hibernate-custom-types

英文:

For later reference, creating custom types in Hibernate will be a great choice to make external library integratable into Hibernate-managed entities.

https://www.baeldung.com/hibernate-custom-types

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

发表评论

匿名网友

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

确定