[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] Load mapping file at "runtime"
|
Den 05.10.2010 17:35, skrev Tom Ware:
Hi Christian,
Is it possible to have multiple persitence units that have the same
entities, but different orm xml files that override the mappings and
just select the persistence unit that has the override you need.
It is possible to alter the underlying EclipseLink metadata after your
peristence unit is created, but you need to be careful because there is
quite alot of code that runs at initialization time to make sure
everything is consistence. Changing the field a BasicMapping maps to
should be fairlyl easy, but if you get into changing relationships
mappings or mappings within InheritanceHierarchies, you will have to be
quite careful.
org.eclipse.persistence.jpa.JpaHelper has a getServerSession() method
that can get our underlying session. Session has a project and project
has a list of mappings that can be acquired by name. Changing the
mappings is a matter of calling the appropriate methods on the various
mappings you get back.
I'm having some trouble getting this all to work.
I do like this:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("OverridesPU");
EntityManager em = emf.createEntityManager();
Project p = JpaHelper.getServerSession(emf).getActiveSession().getProject();
ClassDescriptor cd = p.getClassDescriptor(ZenOrder.class);
From here I've tried to approaches, one is to create a new mapping via
DatabaseMapping and the other is to modify an existing mapping.
To modify an existing mapping I tried:
DirectToFieldMapping dm = (DirectToFieldMapping)
cd.getMappingForAttributeName("paymentOrderId");
dm.setFieldName("dibs_transid"); // the field is not in use on any
mapping. Just testing and have only mapped a small portion of the table.
Then all I get are null values, if I remove the last line I get one of
the other columns. And if I add dm.setGetMethodName("...") and the same
for the setSet-method I get a nullpointerexception.
And if I try to add a DirectToFieldMapping for a @Transient value;
DirectToFieldMapping dm = new DirectToFieldMapping();
dm.setFieldName("zen_orders.dibs_transid");
dm.setAttributeName("paymentOrderId");
cd.addMapping(dm);
I get the following exception:
Exception in thread "main" java.lang.NullPointerException
at
org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(Helper.java:1164)
at
org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:198)
at
org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1368)
at
org.eclipse.persistence.mappings.DatabaseMapping.readFromRowIntoObject(DatabaseMapping.java:1259)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:332)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:661)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:583)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:552)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:492)
at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:444)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:635)
at
org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:838)
at
org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:464)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:997)
at
org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:675)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:958)
at
org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:432)
at
org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1021)
at
org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2898)
at
org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1225)
at
org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1207)
at
org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1181)
at
org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:453)
at
org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:681)
at overrides.Main.main(Main.java:138)
Java Result: 1
Line 138 is: List<ZenOrder> orders = q.getResultList();
I'm somewhat at a loss as what to try next. I would really like this
work by creating a link from a column to a variable I've marked as
transient. But at this point I'm just hoping I can get this to work
somehow. :-)
--
Kind regards
Christian Michelsen