facebook

Problem with multiple DB in the same hibernate.cfg

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #259986 Reply

    Javier
    Member

    I have a problem with the hibernate generation. I need to use the tool to get one hibernate session that can use tables of four differents DB. I use the DB tool to generate it and add it to the cfg file. The problem is that in the four DB exist a table with the same name so the hib session builder throw this exception:

    org.hibernate.DuplicateMappingException: duplicate import: TblAssystem refers to both com.avaya.pso.co.cce.hibernate.beans.activecontact.TblAssystem and com.avaya.pso.co.cce.hibernate.beans.aspreviewcontactmediastore.TblAssystem (try using auto-import=”false”)

    i dont know how to use it if i use differents packages to store the DB tables (As you can see the TblAssystem was created in the package activecontact and aspreviewcontactmediastore so there are really two diferents classes).

    How can i do this configuration?

    #259987 Reply

    Haris Peco
    Member

    jvargase ,

    You can’t access to more databases from one session.If you want access to more database from one application we have to make more SessionFactory instances (more hibernate.cfg.xml) and access to databases from different sessions

    Best
    Peco

    #260018 Reply

    Javier
    Member

    Thanks for your reply Peco. In this case i need to connect to differents MSSQL databases created in a single machine. The configuration works because i have an user that can connect to all this DB. I solve the problem with the repeated tables by disabling the auto-import feature in the hbm of the repeated tables. But you are right, is better to create multiples session factories and use it one by one.

    Thanks!

    #260020 Reply

    Haris Peco
    Member

    jvargase,

    Hibernate Session Factory can connect to one database – you can set database that access to other databases, but it have to be transparent for hibernate.
    This is hibernate FAQ http://hibernate.org/117.html#A22

    Best
    Peco

    #260026 Reply

    Javier
    Member

    Peco, if i configure 4 session factories and in my Bussiness EJB “touch” the 4 DB but in the last one an exception appear the entire transactions roll back?
    That’s the reason because i configure only one session factory…

    #260027 Reply

    Haris Peco
    Member

    jvargase,

    You decide when your transaction starts and commit/rollback – when you configure more session factories every seession factory has its own session and transaction management, for example
    try {
    session1.beginTransaction();
    // do something in db 1
    session1.getConnection().commit();
    } catch (HibernateException e) {
    session1.getConnection().rollback();
    } finally {
    session1.close();
    }
    try {
    session2.beginTransaction();

    You can decide that rollback if acces to any database fail, of course

    I don’t understand how you configure access to more databases in one hibernate session factory – it is impossible, except you configure dsitributed databases, but again – hibernate configuration is for one database.
    For example : you can set 2 or more parameters hibernate.connection.url, but hibernate will pick one (last) and access to one database.
    You can check parameter (property) with
    getConfiguration(“hibernate.connection.url”) and hibernate will return one database url.It mean that hibernate configuration (session factory) contents description for one database.

    Best
    Peco

    #260044 Reply

    Javier
    Member

    Hi Peco, i am using JBOSS 4 with hibernate 3. The DB-ds.xml is looking for my entire MSSQL-DB (No DatabaseName configured; only the connection string, user, password, dialect…) and the user configurated in this DS is able to read / write 4 diferents DataBases created in this server. I configure the sessionFactory with jndi access and create all the POJOS and DAOs using the DataBase Explorer tool in MyEclipse 5. All the generated DAOs are using the jndi name configured in the sessinFactory, off course. With this conf i am able to use any POJO in my EJB Session Beans (by call the DAO) and if an exception appear the container roll back the entire transaction (I dont need to programatically manage the transaction). With this enviroment is possible to use more than one session factory and the transaction functionality works fine?. I ask because implement the change is very easy… i only need to create the new session factorys and change the jndi path in the DAOs so nothing in my EJBs code must be changed.

    Thanks for your time!

    #260049 Reply

    Haris Peco
    Member

    jvargase,

    Sorry, I didn’t understand.You use one database and more catalogs.It is possible with one session factory.You have to choose different class name for
    table.It is possible on page 3 of ‘Hibernate Reverse Engineering’ wizard

    Best
    Peco

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: Problem with multiple DB in the same hibernate.cfg

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