openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jay McHugh <jaydmch...@gmail.com>
Subject Re: 'Masking' fields in an Entity
Date Tue, 31 Mar 2009 06:16:48 GMT
Thank you for the additional insight.

Switching the DetachState is making things more clear.  I had it set
to a value of "all" before based on some example (not sure where from
though).

Here are answers to your questions - Hopefully by the time I finish
answering them all, I will have it working.

1) It is detached.
2) SInce changing the value of DetachState, I now get an exception.
3) The instance is being converted with JAXB marshaller immediately
after being loaded from the database.  So - no serialize/deserialize.

Here is the full routine that is loading the entity from the database
and marshalling it (slightly simplified):

public String sendXML(String projectID, User user) {
	StringWriter xml = new StringWriter();

	OpenJPAEntityManager em = getPM(entityManager);
	FetchPlan plan = em.getFetchPlan();

	plan.setMaxFetchDepth(0);
	plan.clearFetchGroups();
	plan.clearFields();

	plan.addField(Project.class, "projectID");
	plan.addField(Project.class, "projectNumber");
	plan.addField(Project.class, "projectVersion");
	plan.addField(Project.class, "projectRevision");
	plan.addField(Project.class, "projectItem");

	Project spec = em.find(Project.class, projectID);
	spec = em.detach(spec);

	try {
		JAXBContext context = JAXBContext.newInstance(spec.getClass());
		Marshaller m = context.createMarshaller();
		m.marshal(spec, xml);
	} catch (JAXBException je) {
		System.out.println("JAXB Exception: " + je.getMessage());
		je.printStackTrace();
	} catch (Exception e) {
		System.out.println("Some other exception doing JAXB conversion: " +
e.getMessage());
		e.printStackTrace();
	}

	System.out.println("XML: " + xml);
		
	return xml.toString();
}

Well, I've gone through your questions - And, I am much closer (maybe)
to where I need to be.  But, the 'm.marshall' call is trying to call
all of the getters of my entity.  So, it is kicking out
IllegalStateExceptions since the fields are unloaded.

If I set the DetachState to something else, would I be able to get it
to simply return blanks/nulls for the unloaded fields?

Thanks again for you help so far,

Jay
On Tue, Mar 31, 2009 at 12:20 AM, Pinaki Poddar <ppoddar@apache.org> wrote:
>
> Hi,
>  You are correct in detaching the instance with the current fetch plan. In theory, now
the detached instance should only have the fields selected by the fetch plan. If that is not
happening then verify the following in order
>  1. Is the instance detached? OpenJPAEntityManager.isDetached(pc)
>  2. What happens if you access and fields that is not supposed to be loaded in the detached
instance?
> Configure openjpa.DetachState [1] with  AccessUnloaded to throw an exception on any
attempt to acess any unloaded field.
>  3. If the detached instane is serialized and deserialized, which fields in the deserialized
object are populated?
>
>  The above steps to ascertain that when JAXB is xmlizing the instance, inadvertently
no fields are getting loaded as a side-effect.
>
>  You have to tell me more about the process that convertes these instances into XML
-- such as when is this process is triggered? Which fields of the managed instance are processed
by this XMLization process? Is there any possibility that a field will get loaded as a side-effect?
Please note that any pc.getXXX() on a managed instance pc will load the field XXX.
>
>
> [1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_detach_behavior
>
> -----
> Pinaki Poddar                      http://ppoddar.blogspot.com/
>
> http://www.linkedin.com/in/pinakipoddar
> OpenJPA PMC Member/Committer
> JPA Expert Group Member
> --
> View this message in context: http://n2.nabble.com/%27Masking%27-fields-in-an-Entity-tp2545263p2561243.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Mime
View raw message