开发者

How can I package methods to work on any database that contains the required tables

I've just recently started using hibernate and so I apologize if this is a stupid question.

I want to package some of my code into a module which different applications can then utilize. For example, 开发者_高级运维a package that handles sending SMS messages. This package does some database work to get basic config data and then might do some transaction logging. This will then be added to applications requiring SMS functionality. The package would therefore require the calling application to contain the specific config and logging tables required by the package.

My problem is that in order to write the hibernate methods that get the config and do the logging it needs to know of the hibernate specific DOA objects. The problem is these objects are only generated when we create the specific application database mapping.


We are in a similar situation, so here's what we do:

  1. All entities are contained in our library and thus the DAOs have full knowlegde of those entities.
  2. If an application needs more fields in an entity, it subclasses the base entity and subclasses the corresponding DAO as well (which is parameterized using generics).
  3. The library doesn't contain any persistence.xml, but our applications define one, listing the library jar using a <jar-file> element. Note that for this to work, all DAOs have to use the same persistence unit, which is defined in that persistence.xml.
  4. The application will have to provide the tables defines in the library, but those can be overwritten using a XML file (named orm.xml in our case). That file is defined in our persistence.xml.

Of course, you can reduce complexity if you don't have to extend your library with application specific code. In that case you can put the persistence.xml into the lib, use a different persitence unit etc.


You don't need to generate your DAOs, you can create them beforehand, together with the mappings, and package both together with your module. Then the required DB tables need to be created at each site so that they conform to the mapping (or in fact they can be automatically generated from the mapping).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜