Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA/Dynamic OneToOne attribute in inherited table(insert new row failed when the OneToOne row is in a a table with inheritance)
JPA/Dynamic OneToOne attribute in inherited table [message #1051378] Sun, 28 April 2013 17:48 Go to next message
Jean-Paul RITZ is currently offline Jean-Paul RITZ
Messages: 1
Registered: April 2013
Junior Member
Mad

Hi

I am using JPA/Dynamic in a project which is very powerful for the purpose of my project. However I found some limitation or issue
I am struggling with a one to one relationship in a inherited table. As long the 1:1 is not in a inherited table it works fine

I putting the full test code I used the issue happen when I insert a new row I got null pointer

Caused by: java.lang.NullPointerException
at org.eclipse.persistence.mappings.ObjectReferenceMapping.getRealAttributeValueFromAttribute(ObjectReferenceMapping.java:329)
at org.eclipse.persistence.mappings.OneToOneMapping.writeFromObjectIntoRowInternal(OneToOneMapping.java:1864)
at org.eclipse.persistence.mappings.OneToOneMapping.writeFromObjectIntoRow(OneToOneMapping.java:1802)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildRow(ObjectBuilder.java:1394)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildRow(ObjectBuilder.java:1382)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:450)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:300)
at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2894)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1797)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1779)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1730)
at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(CommitManager.java:193)
at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:138)
at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4200)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1439)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1529)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:277)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1167)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
... 2 more



JPADynamicHelper helper = new JPADynamicHelper(emf);
DynamicClassLoader dcl = helper.getDynamicClassLoader();

String packagePrefix = "test" + ".";

companyClass = dcl.createDynamicClass(packagePrefix + "Company");
company = new JPADynamicTypeBuilder(companyClass, null, "u_company");
company.setPrimaryKeyFields("sys_id");
company.addDirectMapping("id", String.class, "sys_id");
company.addDirectMapping("name", String.class, "name");

taskClass = dcl.createDynamicClass(packagePrefix + "Task");
task = new JPADynamicTypeBuilder(taskClass, null, "u_task");
incidentClass = dcl.createDynamicClass(packagePrefix + "Incident", taskClass);
incident = new JPADynamicTypeBuilder(incidentClass, task.getType(), "u_incident");
problemClass = dcl.createDynamicClass(packagePrefix + "Problem", taskClass);
problem = new JPADynamicTypeBuilder(problemClass, task.getType(), "u_problem");
changeClass = dcl.createDynamicClass(packagePrefix + "Change", taskClass);
change = new JPADynamicTypeBuilder(changeClass, task.getType(), "u_change");
requestClass = dcl.createDynamicClass(packagePrefix + "Request", taskClass);
request = new JPADynamicTypeBuilder(requestClass, task.getType(), "u_request");

task.setPrimaryKeyFields("sys_id");
task.addDirectMapping("id", String.class, "sys_id");
task.addDirectMapping("name", String.class, "name");
task.addDirectMapping("description", String.class, "description");

incident.setPrimaryKeyFields("sys_id");
incident.addDirectMapping("id", String.class, "sys_id");
incident.addDirectMapping("delivery", String.class, "delivery");
incident.addDirectMapping("test", int.class, "test");


problem.setPrimaryKeyFields("sys_id");
problem.addDirectMapping("id", String.class, "sys_id");

problem.addDirectMapping("budget", double.class, "BUDGET");
problem.addDirectMapping("milestone", Calendar.class, "MILESTONE");

change.setPrimaryKeyFields("sys_id");
change.addDirectMapping("id", String.class, "sys_id");

request.setPrimaryKeyFields("sys_id");
request.addDirectMapping("id", String.class, "sys_id");

employeeClass = dcl.createDynamicClass(packagePrefix + "Employee");
employee = new JPADynamicTypeBuilder(employeeClass, null, "u_employee");
employee.setPrimaryKeyFields("sys_id");
employee.addDirectMapping("id", String.class, "sys_id");
employee.addDirectMapping("firstName", String.class, "firstname");
employee.addDirectMapping("lastName", String.class, "lastname");
employee.addManyToManyMapping("tasks", task.getType(), "u_task_emp");





ClassDescriptor descriptor = task.getType().getDescriptor();
descriptor.getInheritancePolicy().setClassIndicatorFieldName("task_type");
descriptor.getInheritancePolicy().addClassIndicator(task.getType().getJavaClass(), "TASK");

descriptor.setJavaInterface(task.getType().getJavaClass());
helper.addTypes(true, true, task.getType());

descriptor.getInheritancePolicy().addClassIndicator(incident.getType().getJavaClass(), "INCIDENT");

ClassDescriptor descriptor_incident = incident.getType().getDescriptor();
descriptor_incident.getInheritancePolicy().setClassIndicatorFieldName("task_type");
descriptor_incident.getInheritancePolicy().setParentClass(task.getType().getJavaClass());
helper.addTypes(true, true, incident.getType());



descriptor.getInheritancePolicy().addClassIndicator(problem.getType().getJavaClass(), "PROBLEM");
ClassDescriptor descriptor_problem = problem.getType().getDescriptor();
descriptor_problem.getInheritancePolicy().setClassIndicatorFieldName("task_type");
descriptor_problem.getInheritancePolicy().setParentClass(task.getType().getJavaClass());

descriptor.getInheritancePolicy().addClassIndicator(change.getType().getJavaClass(), "CHANGE");
ClassDescriptor descriptor_change = change.getType().getDescriptor();
descriptor_change.getInheritancePolicy().setClassIndicatorFieldName("task_type");
descriptor_change.getInheritancePolicy().setParentClass(task.getType().getJavaClass());

descriptor.getInheritancePolicy().addClassIndicator(request.getType().getJavaClass(), "REQUEST");
ClassDescriptor descriptor_request = request.getType().getDescriptor();
descriptor_request.getInheritancePolicy().setClassIndicatorFieldName("task_type");
descriptor_request.getInheritancePolicy().setParentClass(task.getType().getJavaClass());





helper.addTypes(true, true, problem.getType());
helper.addTypes(true, true, change.getType());
helper.addTypes(true, true, request.getType());
helper.addTypes(true, true, employee.getType());

helper.addTypes(true, true, company.getType());
incident.addOneToOneMapping("company", company.getType(), "sys_id_company");



I have populated company with some rows

to populate incident

EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
DynamicEntity emp = employee.getType().newDynamicEntity();
emp.set("firstName", "Bob");
emp.set("lastName", "Smith");
emp.set("id", SysID.get());

Collection<DynamicEntity> tasks = emp.<Collection<DynamicEntity>> get("tasks");
tasks.add(incident1());

em.merge(emp);
em.getTransaction().commit();

public DynamicEntity incident1() {

EntityManager em = emf.createEntityManager();
DynamicEntity compEntity = company.getType().newDynamicEntity();
DynamicEntity comp = em.find( compEntity.getClass(),"e25e91119c154122bd3f22f77f7be4cd");

DynamicEntity sm = incident.getType().newDynamicEntity();
sm.set("name", "incident2");
sm.set("description", "bad incident");
sm.set("delivery", "Shipping Query Tool");
sm.set("company", comp);
sm.set("test", 50);

sm.set("id", SysID.get());
return sm;
}


Any help appreciate

JP
Re: JPA/Dynamic OneToOne attribute in inherited table [message #1052714 is a reply to message #1051378] Tue, 30 April 2013 12:50 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Why are you not adding the OneToOne mapping with the descriptor's other mappings, but instead waiting until the end. It looks like the mapping is never getting initialized (as you are adding it after you have registered the descriptor). You need to add the mapping before registering the descriptor.



James : Wiki : Book : Blog : Twitter
Previous Topic:No data persisted to database with new JPA-RS libs
Next Topic:Exception in thread "main" java.lang.ExceptionInInitializerError
Goto Forum:
  


Current Time: Fri Oct 31 22:20:01 GMT 2014

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

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