Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Dynamic EclipseLink JPA Clarification(How to get DynamicClassLoader? [EclipseLink+Jersey and Glassfish 3.1])
Dynamic EclipseLink JPA Clarification [message #659629] Mon, 14 March 2011 20:52 Go to next message
raid3n  is currently offline raid3n
Messages: 6
Registered: March 2011
Junior Member
Hi all and excuse my english,

I try to explain my problem: I should manage entities not mapped in persistence.xml. These entities are created runtime so I haven't jpa entities in my web application.

So I was reading about the dynamic entities on http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic
and the example http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/br anches/2.1/trunk/examples/jpa.employee/eclipselink.example.j pa.employee.dynamic/src/example/EmployeeDynamicMappings.java

But I don't know how to get
DynamicClassLoader dcl
because in the above example it's passed on function. So I paste my example code:

@ManagedBean
@Path("/entity")
public class MyDynamicEntity {

	@PersistenceContext(unitName = "data5PU")
	EntityManager em;


	@SuppressWarnings("unchecked")
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String get() {

		DatabaseSession session = new DatabaseSessionImpl();

		session.setProperty(PersistenceUnitProperties.WEAVING, "true");
		// session.setProperty(PersistenceUnitProperties.WEAVING_FETCHGROUPS,
		// "true");
		session.login();

		DynamicClassLoader dcl = DynamicClassLoader.lookup(session);

		Class<?> aziendaClass = dcl.createDynamicClass("entity.Azienda");
		JPADynamicTypeBuilder builder = new JPADynamicTypeBuilder(aziendaClass,
				null, "D_AZIENDA");

		builder.addDirectMapping("id", int.class, "A_ID");
		builder.addDirectMapping("nome", String.class, "A_NAME");
		builder.setPrimaryKeyFields("A_ID");
		DynamicType type = builder.getType();
		DynamicHelper dynamicHelper = new JPADynamicHelper(em);
	
		DynamicEntity l = null;
		l = (DynamicEntity) em.find(type.getJavaClass(), 0);
		return l.get("A_NAME");

	}}


But in this way I got this error:
JACC Policy Provider: Failed Permission Check.. - permission((org.osgi.framework.AdminPermission (id=220) resolve,resource))

Any suggestions to resolve? What's wrong?


[Updated on: Tue, 15 March 2011 08:53]

Report message to a moderator

Re: Dynamic EclipseLink JPA Clarification [message #659640 is a reply to message #659629] Mon, 14 March 2011 21:44 Go to previous messageGo to next message
raid3n  is currently offline raid3n
Messages: 6
Registered: March 2011
Junior Member
Ok my first error is that:

Quote:
Note: Container managed persistence not supported


Anyway I don't know what's currentLoader in this row in the tutorial

Quote:
DynamicClassLoader dcl = new DynamicClassLoader(currentLoader);

[Updated on: Mon, 14 March 2011 21:45]

Report message to a moderator

Re: Dynamic EclipseLink JPA Clarification [message #659728 is a reply to message #659640] Tue, 15 March 2011 11:23 Go to previous messageGo to next message
raid3n  is currently offline raid3n
Messages: 6
Registered: March 2011
Junior Member
These are my progress:

I added the following line into server.policy
permission java.lang.RuntimePermission    "createClassLoader";

and in this way i don't have the permission exception CreateClassLoader

I paste mine test code, supposing I have a table called "Azienda(id integer,nome varchar)" and wanting to create a dynamic entity class called "entity.Azienda"

@Path("/entity")
public class MyDynamicEntity {

	 
	EntityManager em; 

	@SuppressWarnings("unchecked")
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String get() {		 
		
		Map<String, Object> properties = new HashMap<String, Object>();		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("data5PU", properties);
		em = emf.createEntityManager();		
		Session session = JpaHelper.getEntityManager(em).getSession();
		DynamicClassLoader dcl = DynamicClassLoader.lookup(session);		
		Class<?> aziendaClass = dcl.createDynamicClass("entity.Azienda");
		JPADynamicTypeBuilder builder = new JPADynamicTypeBuilder(aziendaClass,
				null, "Azienda");

		 
		
		builder.addDirectMapping("id", Integer.class, "id");
		builder.addDirectMapping("nome", String.class, "nome");
		builder.setPrimaryKeyFields("id");
		
		 
		DynamicEntity l = null;
		l =  (DynamicEntity) em.find(aziendaClass, 0);
		return l.<String>get("nome");
	}

}


Now the problem is:
Quote:
java.lang.IllegalArgumentException: Unknown entity bean class: class entity.Azienda, please verify that this class has been marked with the @Entity annotation.


The advice and assistance would be greatly appreciated. I do not know what to do yet!!!
Re: Dynamic EclipseLink JPA Clarification [message #666843 is a reply to message #659728] Sat, 23 April 2011 07:11 Go to previous messageGo to next message
Eclipse User
Originally posted by: gregor.karl.frey.t-online.de

You must add the DynamicType to the session using the JPAHelper.
Something like:

DynamicHelper dynamicHelper = new
JPADynamicHelper(getEntityManagerFactory());
dynamicHelper.addTypes(true, true, builder.getType())

The two boolean parameters control the creation of missing tables and
foreign key constraints.

Gregor


On 15.03.2011 12:23, raid3n wrote:
> These are my progress:
>
> I added the following line into server.policy
> permission java.lang.RuntimePermission "createClassLoader";
> and in this way i don't have the permission exception CreateClassLoader
>
> I paste mine test code, supposing I have a table called "Azienda(id
> integer,nome varchar)" and wanting to create a dynamic entity class
> called "entity.Azienda"
>
> @Path("/entity")
> public class MyDynamicEntity {
>
> EntityManager em;
> @SuppressWarnings("unchecked")
> @GET
> @Produces(MediaType.TEXT_PLAIN)
> public String get() {
> Map<String, Object> properties = new HashMap<String, Object>();
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("data5PU", properties);
> em = emf.createEntityManager();
> Session session = JpaHelper.getEntityManager(em).getSession();
> DynamicClassLoader dcl = DynamicClassLoader.lookup(session);
> Class<?> aziendaClass = dcl.createDynamicClass("entity.Azienda");
> JPADynamicTypeBuilder builder = new JPADynamicTypeBuilder(aziendaClass,
> null, "Azienda");
>
>
> builder.addDirectMapping("id", Integer.class, "id");
> builder.addDirectMapping("nome", String.class, "nome");
> builder.setPrimaryKeyFields("id");
>
> DynamicEntity l = null;
> l = (DynamicEntity) em.find(aziendaClass, 0);
> return l.<String>get("nome");
> }
>
> }
>
> Now the problem is:
> Quote:
>> java.lang.IllegalArgumentException: Unknown entity bean class: class
>> entity.Azienda, please verify that this class has been marked with the
>> @Entity annotation.
>
>
> The advice and assistance would be greatly appreciated. I do not know
> what to do yet!!!
Re: Dynamic EclipseLink JPA Clarification [message #903395 is a reply to message #666843] Thu, 23 August 2012 13:26 Go to previous messageGo to next message
Chirag Patel is currently offline Chirag Patel
Messages: 1
Registered: August 2012
Junior Member
Hi Gregor

I understand that JPA entity extensions can be used in Java EE environment. Can I also use "Dynamic EclipseLink JPA" in Java EE (glassfish) environment?

Basically, I want to add mapping to an existing JPA entity class at runtime using "Dynamic EclipseLink JPA" in glassfish environment.

Thanks in advance
Chirag
Re: Dynamic EclipseLink JPA Clarification [message #905007 is a reply to message #903395] Wed, 29 August 2012 14:50 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Yes, Dynamic EclipseLink JPA should work in JEE and Glassfish.

James : Wiki : Book : Blog : Twitter
Previous Topic:Named Query not found
Next Topic:Converter is not called on load operation
Goto Forum:
  


Current Time: Mon Oct 20 11:24:46 GMT 2014

Powered by FUDForum. Page generated in 0.12598 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software