简体   繁体   English

Spring JPA Annotations和ORM.xml替代?

[英]Spring JPA Annotations & ORM.xml alternative?

I want to design my Microservice in a way i can change Frameworks as easy as possible. 我想以一种我可以尽可能简单地改变Frameworks的方式设计我的微服务。 So i have an Interface for everything that could change in the future, but I can't do that for Entities because I'm not aware of a way of using JPA + Hibernate without using @Entity / @Id .. or declaring a ORM.xml File. 所以我有一个接口,可以在将来改变一切,但我不能为实体做到这一点,因为我不知道如何使用JPA + Hibernate而不使用@Entity / @Id ..或声明ORM.xml文件。 Is there maybe a way to define configuration class(es) which handle Entities? 有没有办法定义处理实体的配置类?

Random-Example: 随机实例:

@Entity 
@IdClass(DemoId.class)
@Table(name = "demo",catalog="demodb")
public class Demo implements Serializable{
    @Id
    private long pk1;
    @Id
    private long pk2;
    @Id
    private long pk3;    
    @Lob
    String description; 

    @ElementCollection(targetClass=String.class)
    List<String> infos = new ArrayList<>(); 
}

public class DemoId implements Serializable{   
    private long pk1;
    private long pk2;
    private long pk3;    
}

How could I separate my Entities (in this case Demo ) and the JPA-Annotations in two Classes? 我如何在两个类中分离我的实体(在本例中为Demo )和JPA-Annotations? So if there ever would be something that makes JPA Deprecated I could easy switch (and just change 1 adapter or something like that) 所以,如果有一些东西让JPA贬值我可以轻松切换(只需更换1个适配器或类似的东西)

Ty in advanced Ty先进

From my understanding and experience with JPA, it's impossible to have the best of both worlds with this issue. 根据我对JPA的理解和经验,在这个问题上不可能拥有两全其美。 The JPA relies on the Annotations directly inside the file. JPA直接依赖于文件内的Annotations。 The best alternative you have as I see it, is to create two separate .java files for each. 我看到的最好的替代方法是为每个创建两个单独的.java文件。 It's not a pretty solution but I don't think you'll get anything better. 这不是一个漂亮的解决方案,但我不认为你会得到更好的东西。

ex: File #1 DemoAnnotated.java 例如:文件#1 DemoAnnotated.java

@Entity 
@IdClass(DemoId.class)
@Table(name = "demo",catalog="demodb")
public class Demo implements Serializable{
    @Id
    private long pk1;
    @Id
    private long pk2;
    @Id
    private long pk3;    
    @Lob
    String description; 

    @ElementCollection(targetClass=String.class)
    List<String> infos = new ArrayList<>(); 
}

File #2 Demo.java 文件#2 Demo.java

public class Demo implements Serializable{
    private long pk1;
    private long pk2;
    private long pk3;    
    String description; 
    List<String> infos = new ArrayList<>(); 
}

You'll probably save time in the long run to just keep the annotations for now. 从长远来看,你可能会节省时间来保留现在的注释。 If you need to quickly switch over frameworks later on - it will require refactoring but you should be able to do it rather quickly. 如果您需要稍后快速切换框架 - 它将需要重构,但您应该能够快速地完成。 I can't imagine the changes being too intense. 我无法想象变化太激烈了。 Might simply be a @Entity -> @SomeOtherName which in that case you can do a search and replace. 可能只是一个@Entity - > @SomeOtherName,在这种情况下你可以进行搜索和替换。 Search and replace might be your saviour here. 搜索和替换可能是您的救星。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM