[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Getting a database connection withEntityManagerImpl

Thanks Andrei,

I've just done a test with DbUnit and it's working fine. To get a DbUnit connection you just have to do :

import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.DatabaseConnection;
.....
.....
IDatabaseConnection connection = new DatabaseConnection(((EntityManagerImpl) (em.getDelegate())).getServerSession().getAccessor().getConnection());


Thanks,
Antonio


2008/9/19 Andrei Ilitchev <andrei.ilitchev@xxxxxxxxxx>

java.sql.Connection conn = ((EntityManagerImpl)(em.getDelegate())).getServerSession().getAccessor().getConnection();

----- Original Message -----
Sent: Friday, September 19, 2008 5:58 AM
Subject: [eclipselink-users] Getting a database connection withEntityManagerImpl

Hi,

I'm using DbUnit with EclipseLink for unit testing. I sent an email to the DbUnit mailing list wondering if there was a easier way to get a database connection using the information included in a persistence.xml file. Julien Tabouret replied with a hint on the method EntityManager.getDelegate(). His example (see below) uses Hibernate.

With EclipseLink the EntityManager.getDelegate() method returns an org.eclipse.persistence.internal.jpa.EntityManagerImpl object. I can't find any documentation about this class (http://www.eclipse.org/eclipselink/api/1.0.1/index.html). 

Do you know how I can get a database connection (to give to dbunit) with the EntityManagerImpl object ?

Thanks,
Antonio

---------- Forwarded message ----------
From: julien tabouret <julien.tabouret@xxxxxxxxx>
Date: 2008/9/17
Subject: Re: [dbunit-user] Persistence.xml
To: dbunit-user@xxxxxxxxxxxxxxxxxxxxx


Hello,

If you have a persistence.xml file, I assume that you're using an EntityManager. If so, the getDelegate() method could help you since it retrieves the underlying object for the EntityManager.

For example, if your O/R mapping provinder is Hibernate, you will get an org.hibernate.impl.SessionImpl. Then the connection() method will gives you the JDBC connection required by dbunit.

Hope it can help.
--
Julien

2008/9/17 Antonio Goncalves <antonio.mailing@xxxxxxxxx>
Hi,

I was wondering if DBUnit has some facilities to integrate a persistence.xml file. In the persistence.xml file file there is already the JDBC Driver, the connection URL to the database, login, pwd... everthing needed to connect to a DB. What I'm doing at the moment is duplicating this information into my code sych as :

protected IDatabaseConnection getConnection() throws Exception {
        Class driverClass = Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection jdbcConnection = DriverManager.getConnection("jdbc:derby:myDB.sql;create=true", "APP", "APP");
        return new DatabaseConnection(jdbcConnection);
    }

it would be nice just to have new DatabaseConnection("persistence.xml"). Because the properties change depending on the prodiver, we could also have something like w DatabaseConnection("persistence.xml", ORACLE).

Any ideas ?

--
Antonio Goncalves (antonio.goncalves@xxxxxxxxx)
Software architect

Paris JUG leader : www.parisjug.org
Web site : www.antoniogoncalves.org
Blog: jroller.com/agoncal
LinkedIn: www.linkedin.com/in/agoncal

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url="">
_______________________________________________
dbunit-user mailing list
dbunit-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/dbunit-user


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url="">
_______________________________________________
dbunit-user mailing list
dbunit-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/dbunit-user




--
--
Antonio Goncalves (antonio.goncalves@xxxxxxxxx)
Software architect

Paris JUG leader : www.parisjug.org
Web site : www.antoniogoncalves.org
Blog: jroller.com/agoncal
LinkedIn: www.linkedin.com/in/agoncal


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users




--
--
Antonio Goncalves (antonio.goncalves@xxxxxxxxx)
Software architect

Paris JUG leader : www.parisjug.org
Web site : www.antoniogoncalves.org
Blog: jroller.com/agoncal
LinkedIn: www.linkedin.com/in/agoncal