简体   繁体   中英

How to use same domain class for HBase and PostgreSQL

I have domain objects that are stored in both HBase and 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. I use Hibernate/Spring framework.

For PostgreSQL, the domain class I use is as below: (snippets)

@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:

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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