Announcement Announcement Module
Collapse
No announcement yet.
howto copy object multiple databases w/ multiple Hib. DAOs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • howto copy object multiple databases w/ multiple Hib. DAOs

    Hi all. I'm having problems copying instances of an object from one database to another using Hibernate and Spring. Has anyone done this successfully? If so, how?

    Right now I'm trying to make a deep copy of an object (using Serialization), ensure that it exists in the target db, and then call saveOrUpdate() with that object on the target dao. It seems to work correctly sometimes, but at other times it is entirely flaky or incorrect.

    I suppose I could manually load the target object into the Session and then copy the values over to it one by one, but this seems tedious.

    This is what my current code looks like:

    public MappedNode pushNodeToDB(MappedNode nodeToPush, NodeDAO dao) throws Exception {
    boolean nodeExists = dao.getNodeExistsWithId(nodeToPush.getNodeId());
    MappedNode otherNode;
    if (!nodeExists) {
    dao.addNodeWithId(nodeToPush.getNodeId());
    }
    otherNode = (MappedNode) ObjectCloner.deepCopy(nodeToPush);
    if (otherNode.getSynonyms() != null) {
    otherNode.setSynonyms(new TreeSet(otherNode.getSynonyms()));
    }
    dao.saveNode(otherNode);
    return otherNode;
    }

    It's theoretically sound, it just doesn't work!

    Thanks in advance for any help,
    Danny

  • #2
    Why do you want to do an object copy using Serialization? For UserType defined objects and for the relations between objects I think you'll have some problems - especially since HB uses CGlib proxies for lazy initialization.
    I'm not saying that it can't be possible but I think you'll have to know more your db and HB internals.
    Why don't you try something more easier (ofc I assume it depends on your particular context but anyways):
    1. simply copy the database - make a dump and then import it into the other db
    2. load the object from db A, attach it to the SessionFactory of db B and then save it there (using save or update). The only problem is parsing the relation tree but you can do that in several ways:
    a. simply load the objects and parse all the relations-ships while saving each node individually.
    b. load w/o lazy loading the parents nodes in your database and save them using cascaded-saving (this should take care of at least one set of relations ).

    The second approach should run faster but in case of entities with more then one relation it might prove problematic. Search also the HB forums - for sure there are some good advices there (if you cannot find anything useful on the HB wiki).

    Comment

    Working...
    X