-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Obtaining the datasource name from persistence.xml
PostPosted: Fri Dec 02, 2011 8:29 am 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
Good morning guys,

I'm working in a system that mostly uses JPA2 to work with persistence. but in some cases, we need to execute some gigantic select queries and map them to a DTO (not using any entity).

the JPA way of doing it is to execute a nativeQuery and map the resultSet using @SqlResultSetMapping but this annotation is incredibly cumbersome.

So I'm trying to implement a way to get a connection and execute it through the JDBC way, and then I'll be using apache dbutils to map the resultSet.

The problem: I'd like to obtain this connection from the entityManager, or at least get the datasource configuration from persistence.xml.
So I was thinking in 3 possible solutions with the following questions:

1) unwrap the connection (probably I'd have to be inside a transctional context, but anyway, how can I do that with hibernate?)
2) get the datasource name from jpa api, something like: entityManagerFactory.getProperties().get("datasource.name") (this don't work though, so is there another way to get it?)
3) in case hibernate provides a different way to map the resultset, forget this approach and use this feature. (does hibernate provides something like this?)


Top
 Profile  
 
 Post subject: Re: Obtaining the datasource name from persistence.xml
PostPosted: Fri Dec 02, 2011 8:42 am 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
how can I access the PersistenceUnitInfo ?
http://docs.oracle.com/javaee/6/api/jav ... tInfo.html


Top
 Profile  
 
 Post subject: Re: Obtaining the datasource name from persistence.xml
PostPosted: Mon Dec 05, 2011 12:52 pm 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
Good morning,

Maybe I could use the nativeQuery with another tool to use the resultSetMapping, something similar to what apache dbutils provides.
Do you guys know anything like that?


Top
 Profile  
 
 Post subject: Re: Obtaining the datasource name from persistence.xml
PostPosted: Mon Dec 05, 2011 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 11, 2009 2:26 am
Posts: 29
if you're using hibernate, then try unwarp a session, and do session.doWork(), this method requires a Work intance, which will give you a connection


Top
 Profile  
 
 Post subject: Re: Obtaining the datasource name from persistence.xml
PostPosted: Mon Dec 05, 2011 2:23 pm 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
Hi Stliu,

What is a "Work" exactly in hibernate context?

I was trying to create a CDI producer to provide a datasource OR a connection to someone using like:
Code:
@Inject
DataSource dataSource
//OR
@Inject
Connection connection


that datasource/connection would have to come from the same context as an entityManager
so, using work, could I try something like this to obtain the connection?
Code:
    @Inject
    private EntityManager entityManager;

    private Connection connection;

    @Produces
    public Connection getConnection() {

        org.hibernate.Session session = entityManager.unwrap(Session.class);
        session.doWork(new Work() {
            public void execute(Connection con) throws SQLException {
                connection = con;
            }

        });

        return connection;
    }



I feel that something is not in place. Besides that with I'm getting a weird exception when using this producer

http://pastie.org/2970661


Top
 Profile  
 
 Post subject: Re: Obtaining the datasource name from persistence.xml
PostPosted: Mon Dec 05, 2011 4:27 pm 
Newbie

Joined: Thu Nov 18, 2010 2:12 pm
Posts: 7
I understood that is for the best to not obtain the connection but to run a work inside the doWork method.
I won't provide the connection to an unbound operation. instead I'll use it as the api suggests.

thank you Stliu.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.