本文介绍了EclipseLink JPA 2.1用户提供的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用EclipseLink作为我的JPA引擎。
我们使用java.sql.Connection的子类,我想知道我是否可以强制EclipseLink使用我的连接。我想以编程方式设置连接对象。



一样

  MyConnection conn = new MyConnection(JDBC_URL,USR,PWD); 
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn);


解决方案

您可以使用以下代码:

 地图属性= new HashMap(); 

//配置内部EclipseLink连接池
properties.put(JDBC_DRIVER,oracle.jdbc.OracleDriver);
properties.put(JDBC_URL,jdbc:oracle:thin:@localhost:1521:ORCL);
properties.put(JDBC_USER,user-name);
properties.put(JDBC_PASSWORD,password);
Persistence.createEntityManagerFactory(unit-name,properties);

我在google搜索后找到了这个答案: >



我不相信有一个强制Eclipselink使用 java.sql.Connection ;不是至少开箱。

  public Map convertConnectionToProperties()方法是使用一个简单的方法来传递上述连接, Connection conn){
//做映射

返回map;
}

如果您查看,您将看到



Persistence.java



  createEntityManagerFactory(String persistenceUnitName)
createEntityManagerFactory(String persistenceUnitName,Map properties)

这是所有可以作为参数。简单的说;你所要求的只是不可能没有采取你的连接对象,并调整它返回符合 createEntityManagerFactory 可以接受的东西。

  class MyConnection {
//您的代码在这里

public Map convertToMap(){
// Do映射

返回map;
}

public String getPersistenceUnitName(){
returnSome-Name;
}
}

然后使用它:

  MyConnection conn = new MyConnection(JDBC_URL,USR,PWD); 
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn.getPersistenceUnitName(),conn.convertToMap());


I would like to use EclipseLink as my JPA engine.We are using a subclass of java.sql.Connection and I would like to know if I can force EclipseLink to use my connection. I would like to set the connection object programmatically.

Something like

MyConnection conn = new MyConnection(JDBC_URL, USR, PWD);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn);
解决方案

You can set a custom connection programatically using the following code:

Map properties = new HashMap();

// Configure the internal EclipseLink connection pool
properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
properties.put(JDBC_USER, "user-name");
properties.put(JDBC_PASSWORD, "password");
Persistence.createEntityManagerFactory("unit-name", properties);

I found this answer here after a quick google search:

http://eclipse.1072660.n5.nabble.com/Defining-persistence-xml-programatically-td2536.html

I dont believe that there is a way to force Eclipselink to connect using only that java.sql.Connection; not out of the box at least. You could make a simple utility method to pass in said connection and build a properties map as I described above.

public Map convertConnectionToProperties(Connection conn) {
    // Do the mapping

    return theMap;
}

If you take a look at this link you will see that

Persistence.java

createEntityManagerFactory(String persistenceUnitName)
createEntityManagerFactory(String persistenceUnitName, Map properties)

That is all it can take as parameters. Simply put; what you are asking for simply isn't possible without taking your connection object and adjust it to return something that complies with what createEntityManagerFactory can take in.

class MyConnection {
     // Your code here

    public Map convertToMap() {
        // Do the mapping

        return theMap;
    }

    public String getPersistenceUnitName() {
        return "Some-Name";
    }
}

And then utilize it like this:

MyConnection conn = new MyConnection(JDBC_URL, USR, PWD);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn.getPersistenceUnitName(), conn.convertToMap());

这篇关于EclipseLink JPA 2.1用户提供的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:16