Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Load mapping file at "runtime"

Hi Christian,

Chis is, of course correct that the recommended way to change mappings is to register a customizer.

We should, however be able to make the addition of a DirectToField mapping work at runtime - I am working on the assumption that you need this to work post-login.

The issue you are likely running into is that you are using configuration that expects the initialize(AbstractSession) method to be run on your mapping. One thing the initialize(AbstractSession) method will do is convert from the field name you have provided to our internal representation of DatabaseField. The lack of that conversion is likely causing the problem. You may be able to just run the initialize(AbstractSession) method and have things work since a DirectToField mapping is fairly self contained. Otherwise, you will have to build up the internal representation of the field. Have a look in the initialize(AbstractSession) method for DirectToField and its immediate superclass to see how that is done.

Adding mappings post-login is not a recommended way of doing things and as such, it will require a bit of tinkering with your code to get it to work.

-Tom


Christopher Delahunt wrote:
Hello Christian,

I'm not sure about the descriptor changes you are trying to make; hopefully someone can comment on those later.  But I thought I would mention that you should not be making descriptor/mapping changes after the session is logged in, since everything will already have been initialized.  You will want to register a customizer with your persistence unit and use it to make the changes, since this runs prior to login/initialization.

Best Regards,
Chris


----- Original Message -----
From: chris-m@xxxxxxx
To: eclipselink-users@xxxxxxxxxxx
Sent: Saturday, October 9, 2010 4:13:10 PM GMT -05:00 US/Canada Eastern
Subject: 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. :-)



Back to the top