facebook

Multiple (two) hibernate session factories ?

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #220273 Reply

    I must handle two databases in an application. Does this mean I cannot use the MyEclipse Hibernate capabilities ?

    Daniel

    #220325 Reply

    Riyad Kalla
    Member

    Daniel,
    I asked our DB dev to look at your original post in the other forum.

    #220541 Reply

    support-jeff
    Member

    No, you can still use them. The easiest thing to do would be to maintain separation between objects that are persisted to one database from those persisted to the other in two eclipse projects. Then you can use the tool for each database independently.

    If your design does not allow this (which I would say is a design flaw then), you just have to do a bit more manual manipulation of things. First off, you will have to create a second hibernate.cfg.xml file (unless Hibernate supports multiple connection definitions in a config file – but I do not believe it does). You can do this using the Hibernate Config wizard – just do not overwrite the first one. Then, you will probably need to adapt the SessionFactory class created by adding Hibernate Capabilities to return the appropriate Session object for the database you want. Lastly, in creating mapping files from the database, you will have to manually move the mapping references in the config file around. Be sure to remove those moved from the .myhibernatedata file in your project. Otherwise, they are put back in the config file when it gets regenerated (alternately, chose not to update the config file when generating mappings). Make sense?

    #220550 Reply

    Oooooo !
    Not so fast, there, or I’ll be blown away…

    The first paragraph of your answer suggests something that I feel, somewhat, uncomfortable with
    Imagine the application is watching database A (Informix SE) and, when an interval expires or on request, extracts some fields from a few of its tables. It, then, assembles the fields into new records and inserts these into database B (MySQL).
    Essentially, this is my whole problem.
    Now, how do I do this in two separate projects ? I can come up with some, very convoluted, ways that would involve RMI or such stuff, but other than that … no clue, sorry.
    So, the design flaw is my cluelessness, I guess. However, I would apreciate any suggestions !
    For the second part – I was affraid it might work :-). I’m going to try… Don’t expect results too soon, though. I doesn’t look like a walk in any park !

    Daniel

    #220551 Reply

    Riyad Kalla
    Member

    Daniel,
    I don’t believe Jeff was suggesting two projects, just two hibernate configurations. hibernate-informix.cfg.xml and hibernate-mysql.cfg.xml, Both define the objects specific to each database and that’s that. Jeff was also suggesting that you will need to modify the session factory utility class to provide Sessions for each Hibernate configuration, not just one. This is very dooable in 1 project, it just requires some good old fashion elbow greese.

    I’d suggest grabbing Hibernate in Action, and reading the first 4 chapters… it might take you 1.5 hours to do, but the whole idea of how Hibernate works and the separation of your two DBs using Hib should be a heck of a lot clearer. Every chapter after that is detail into some feature or another.

    #220570 Reply

    Thans, Riyad.
    I’m going to follow your advice and get the book. After you suggested it, I looked through the contens and found it would be worth having.
    I don’t exactly mind the “old fasioned elbow grease”, if (and only if) ther is no way around it. I was hoping the way around was the Hibernate plugin in MyEclipse. It turns out it was, but not entirely.

    By the way, Jeff did say “two projects”. Literally.
    I’d still like to know if he had something speciffic in mind in that respect – especially after saying it was a “design flaw” not to be able to do two projects – see the quote bellow.

    Thanks, again.
    As I said before, I wont be able to post updates on this for a while. I expect to be busy learning and doing things.

    Daniel

    Quote: =============
    No, you can still use them. The easiest thing to do would be to maintain separation between objects that are persisted to one database from those persisted to the other in two eclipse projects. Then you can use the tool for each database independently.

    If your design does not allow this (which I would say is a design flaw then), you just have to do a bit more manual manipulation of things.
    ============== End quote

    #220575 Reply

    Riyad Kalla
    Member

    Ahhh Daniel I appologize, you are right. I guess what Jeff might have meant is to create two separate Java Projects, one for each set of Hibernate objects. Add Hibernate Capabilities to each project (be sure to name the hibernate.cfg.xml files something different from eachother) and then in your master web project I suppose you could make the two Java Projects dependencies of the Web Project that get packaged up and deployed with it. You also might want to name the SessionFactories something different as well.

    I will ask him for clarification on his “Design flaw” comment.

    #220588 Reply

    support-jeff
    Member

    By “design flaw” I mean that the domain model might be so tightly coupled that you cannot separate objects coming from different persistence stores.

    However, in your case, it seems you are using the second store as a failover option, not a source of one part of the data for your application. In this case, objects appear to be truly persisted to two different databases. Am I reading that correctly? I have never come across this particular use case before.

    In this case, I think you *are* better off keeping things in one project, and have the SessionFactory track two sessions, one to each database.

    #220636 Reply

    Thanks, Jeff.

    Well, I don’t know if my explanation was clear enough, since you say you never came across such a case. What I have to do is a modified form of a database conversion application.
    My problem is that I must deal with two stores, in such a case. And I thought this would be quite common, in real life: you get some data from where it currently resides and use it in a new application, whith it’s own store.
    I am not allowed to use the initial database, nor do I want to (Informix SE over CISAM …).
    I don’t want to keep this on forever. If you think you may help further and can aford the time, I certainly would be curious hear your thoughts. But, if you don’t, I won’t mind – maybe I better try to figure some of this on my own.

    Thanks, again.

    Daniel

    #221851 Reply

    manoharan
    Member

    Respected sir,
    Please send me details of hibernate in action – first 4 chapters.. to my emailid manochandar2003@yahoo.com
    Daniel,
    I don’t believe Jeff was suggesting two projects, just two hibernate configurations. hibernate-informix.cfg.xml and hibernate-mysql.cfg.xml, Both define the objects specific to each database and that’s that. Jeff was also suggesting that you will need to modify the session factory utility class to provide Sessions for each Hibernate configuration, not just one. This is very dooable in 1 project, it just requires some good old fashion elbow greese.

    I’d suggest grabbing Hibernate in Action, and reading the first 4 chapters… it might take you 1.5 hours to do, but the whole idea of how Hibernate works and the separation of your two DBs using Hib should be a heck of a lot clearer. Every chapter after that is detail into some feature or another.
    _________________
    Riyad
    MyEclipse Support

    #221909 Reply

    I’m afraid there aren’t many details I can send, other than the book’s website address:

    https://secure.manning.com/bauer

    Essentially, you go to the site and buy the book in the format that best suits your taste and reading habits.

    Daniel

Viewing 11 posts - 1 through 11 (of 11 total)
Reply To: Multiple (two) hibernate session factories ?

You must be logged in to post in the forum log in