开发者

insert hibernate domain objects to different database?

I want to copy all data of a specific table from database1 to database2. In my system i have access via hibernate to the domain object from database1, i don't have to transform the data-structure. i have only a native jdbc connection to database2.

whats the best solution to make this groovy script very generally to support all kinds of domain objects i have? so this开发者_如何学编程 script only gets my domain object and the connection string to database and inserts all the data?


I faced a similar issue where I needed the ability to export every hibernate entity to an SQL script, in other words if you had a Person object with two properties (username, password) you should be able to generate the SQL insert statement of that Object.

Person.username = x Person.password = y

then the process would extract from that object the equivalent SQL insert and create something like:

insert into person (username, password) values ('x', 'y');

However my solution was based on the fact that mappings are done using hibernate annotations and not XML configuration, if this is your case you could achieve the same with 1 or 2 working days, just read the annotations. noting that you will have to do an extra step which is executing the resulted SQL inserts on the other DB.

FYI: this method toSQL() was added in a superclass (AbstractHibernateEntity) that every hibernate entity extended, so calling it was the easiest thing to do.

This was the complicated solution and most general one, however if you only need to copy one table from DB to another I would suggest to simple go with a simple JDBC call and avoid complicating your life ;-)

Regards.


Maybe the easiest would be to stick on the technology level that is common to both databases.

If they exist, you could use database-specific commands, that would be really fast.

If not, you could use simple jdbc on both. You could that in a generic way :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜