ERLEDIGT
JA
JA
ANTWORTEN
1
1
ZUGRIFFE
795
795
EMPFEHLEN
-
Hi,
ich habe es nun geschafft, (ehm Problem aus alten Thread) meine Entities zu holen und den Namen der Persistence Unit.
Wie kann ich mit Hilfe des PU Namen den Entity Manager -> by Name <- holen ?
Ich verwende in meinem EAR eine persistence.xml mit PU dwh-inventory
Wie bekomme ich nun aus dem context den passenden Entity Manager ohne Dependency Injection (Sprich @PersistenceContext) ?
Danke im voraus und Danke nochmals an Thomas Darimont für die bisherige Hilfe!
-
Habe das Problem nun selbst gelöst :
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
package com.controlware.nocdwh.dao.spi; import java.net.URL; import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.SessionContext; import javax.ejb.Stateful; import javax.jws.WebMethod; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * Session Bean implementation class PersistenceUnitCanOpener */ @Stateful public class PersistenceUnitCanOpener implements PersistenceUnitCanOpenerLocal { @Resource SessionContext ctx; @PersistenceContext(unitName="etlmw") protected EntityManager etl; @PersistenceContext(unitName="inventory") protected EntityManager inv; private Map<String, EntityManager> emHash = new HashMap<String, EntityManager> (); public Collection<Class<?>> importProcessIdAttached = null; /** * Default constructor. */ public PersistenceUnitCanOpener() { // TODO Auto-generated constructor stub } public HashMap<Class<?>, EntityManager> class2EM=null; @PostConstruct public void initDatasources() { try { emHash.put("nocdwh-etlmw", etl); emHash.put("nocdwh-inventory-etlmw", inv); class2EM = readFromPersistenceUnit(emHash); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } } public EntityManager getEntityManager4Class(Class<?> clazz) { return class2EM.get(clazz); } public HashMap<Class<?>,EntityManager> readFromPersistenceUnit(Map<String, EntityManager> availableEntityManagers) throws Exception { HashMap<Class<?>,EntityManager> result= new HashMap<Class<?>, EntityManager>(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); Enumeration<URL> urls = cl.getResources("META-INF/persistence.xml"); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (urls.nextElement().openConnection().getInputStream()); // normalize text representation doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList nl = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) { Node node = nl.item(i); if ("persistence-unit".equals(node.getNodeName())) { Node puNode = node.getAttributes().getNamedItem("name"); String name = puNode.getNodeName(); System.out.println("PU : "+puNode); NodeList pl = node.getChildNodes(); for (int j = 0; j < pl.getLength(); ++j) { Node model = pl.item(j); if ("class".equals(model.getNodeName())) { Node clazz=model.getFirstChild(); String modelName = clazz.getNodeValue(); System.out.println(name + " -> " + modelName); Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(modelName); result.put(clz, availableEntityManagers.get(name)); } } } } return result; } }
Ähnliche Themen
-
problems with entity beans and persistence.xml
Von enrix im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 3Letzter Beitrag: 21.10.09, 22:28 -
Web-Applikation mit Persistence-Unit
Von AAF im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 3Letzter Beitrag: 28.11.08, 07:58 -
Hibernate - Persistence Unit Not Found?
Von flashray im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 4Letzter Beitrag: 30.06.08, 11:01 -
persistence unit funktioniert nicht
Von ma_ria im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 0Letzter Beitrag: 16.04.08, 13:57 -
Wert ausserhalb eines Frames holen
Von ddhb im Forum Javascript & AjaxAntworten: 1Letzter Beitrag: 09.12.05, 17:55





Zitieren
Login





