简体   繁体   English

如何为HBase和PostgreSQL使用相同的域类

[英]How to use same domain class for HBase and PostgreSQL

I have domain objects that are stored in both HBase and PostgreSQL. 我有存储在HBase和PostgreSQL中的域对象。

While defining the class the annotations used for PostgreSQL are not applicable to HBase, so I end up defining two classes with the same properties but different annotations. 在定义类时,用于PostgreSQL的注释不适用于HBase,因此我最终定义了两个具有相同属性但注释不同的类。 I use Hibernate/Spring framework. 我使用Hibernate / Spring框架。

For PostgreSQL, the domain class I use is as below: (snippets) 对于PostgreSQL,我使用的域类如下:(代码段)

@Entity
@Table(name="foo_bar")
public class FooBarPgsql implements Serializable {
    private static final long serialVersionUID = 5848293352035620189L;

    @Id
    @Column(name="id")
    String id;

    @Column(name="name")
    String name;
}

for HBase, the class is: 对于HBase,该类是:

public class FooBarHbase {
    String id;
    String name;
}

So, If I had to use a single class for both PostgreSQL and HBase, how should I define the class? 因此,如果我必须对PostgreSQL和HBase使用单个类,那么我应该如何定义该类?

I think your combined class is going to be more complicated than two classes with a common interface (which can be dispatched as necessary). 我认为您的组合类将比具有公共接口(可以根据需要调度)的两个类更加复杂。 The databases are just so different, and HBase is not really compatible with ORM approaches. 数据库是如此不同,并且HBase确实与ORM方法不兼容。

I think you will get more bang for your buck with careful use of composition and the like than you will out of trying to fully merge the classes. 我认为,与不尝试完全合并类相比,通过谨慎地使用合成等,您将获得更多收益。 Since Java can dynamically alter class properties, trying to centralize code in this way is more likely to allow you a clean set of classes for delegating the actual database handling. 由于Java可以动态更改类属性,因此尝试以这种方式集中化代码更有可能允许您使用一组干净的类来委派实际的数据库处理。

You can probably do this if you really really want, but I am not sure it will be worth it in terms of the cost it will certainly take in terms of code quality. 如果您确实想要,可以这样做,但是我不确定在代码质量方面肯定要付出的代价是否值得。 Let details depend on interfaces, not the other way around. 让细节取决于接口,而不取决于接口。

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

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