Dynamic EclipseLink JPA Clarification [message #659629] |
Mon, 14 March 2011 16:52  |
Eclipse User |
|
|
|
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 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 04:53] by Moderator
|
|
|
|
Re: Dynamic EclipseLink JPA Clarification [message #659728 is a reply to message #659640] |
Tue, 15 March 2011 07:23   |
Eclipse User |
|
|
|
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 03:11   |
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!!!
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03981 seconds