Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Virtual Metlhods | questions

Tom,
  One step further.

  I created the OneToOneMapping as follows 

	private void oneToOneMapping(ClassDescriptor classDescriptor) {
		OneToOneMapping oneToOneMapping = new OneToOneMapping();
		oneToOneMapping.setDescriptor(classDescriptor);
		oneToOneMapping.setCascadeAll(true);
		oneToOneMapping.useBasicIndirection();
		
		VirtualAttributeAccessor virtualAttributeAccessor2 = new
VirtualAttributeAccessor();
		virtualAttributeAccessor2.setAttributeName("phoneNumber");
		virtualAttributeAccessor2.setGetMethodName("get");
		virtualAttributeAccessor2.setSetMethodName("set");
		virtualAttributeAccessor2.setValueType(ValueHolderInterface.class);
	
virtualAttributeAccessor2.initializeAttributes(classDescriptor.getJavaClass());
		
		oneToOneMapping.setAttributeAccessor(virtualAttributeAccessor2);
		oneToOneMapping.setAttributeName("phoneNumber");
		oneToOneMapping.setReferenceClass(PhoneNumber.class);
		oneToOneMapping.addForeignKeyFieldName("TLG_GC_EMPLOYEE.PHONE_NUMBER_ID",
"TLG_GC_PHONE.ID");
		
		
		classDescriptor.getMappings().add(oneToOneMapping);
	}

and in the Employee entity set/get as (just test code may not 100 accurate)

	
	public Object get(String key) {
		
		return ValueHolderInterface.class.isInstance(extensions.get(key)) ?
ValueHolderInterface.class.cast(extensions.get(key)).getValue() :
extensions.get(key);
	}
	
	public Object get(String key,ValueHolderInterface valueHolderInterface) {
		
		return ValueHolderInterface.class.isInstance(extensions.get(key)) ?
ValueHolderInterface.class.cast(extensions.get(key)).getValue() :
extensions.get(key);
	}

public void set(String key,Object value) {
		if (PhoneNumber.class.isInstance(value)) {
			ValueHolderInterface valueHolderInterface = new ValueHolder();
			valueHolderInterface.setValue(value);
			extensions.put(key, valueHolderInterface);
			
		} else {
			extensions.put(key, value);
		}
	}
	
	public void set(String key,ValueHolderInterface value) {
		if (PhoneNumber.class.isInstance(value)) {
			ValueHolderInterface valueHolderInterface = new ValueHolder();
			valueHolderInterface.setValue(value);
			extensions.put(key, valueHolderInterface);
			
		} else {
			extensions.put(key, value);
		}
	}	


I getting the following exception in the insert scenario


Exception in thread "main" javax.persistence.EntityExistsException: 
Exception Description: The mapping for the attribute [phoneNumber] uses
indirection, and so must be initialized to a new ValueHolder.  Currently the
value is: [PhoneNumber(0: 613-5558812)].
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[phoneNumber]
Descriptor: RelationalDescriptor(gridcache.model.Employee -->
[DatabaseTable(TLG_GC_EMPLOYEE)])
	at
org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:443)
	at gridcache.example.InsertExample.main(InsertExample.java:28)
Caused by: Exception [EclipseLink-125] (Eclipse Persistence Services -
2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The mapping for the attribute [phoneNumber] uses
indirection, and so must be initialized to a new ValueHolder.  Currently the
value is: [PhoneNumber(0: 613-5558812)].
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[phoneNumber]
Descriptor: RelationalDescriptor(gridcache.model.Employee -->
[DatabaseTable(TLG_GC_EMPLOYEE)])
	at
org.eclipse.persistence.exceptions.DescriptorException.valueHolderInstantiationMismatch(DescriptorException.java:1771)
	at
org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy.validateAttributeOfInstantiatedObject(BasicIndirectionPolicy.java:389)
	at
org.eclipse.persistence.mappings.ForeignReferenceMapping.getAttributeValueFromObject(ForeignReferenceMapping.java:892)
	at
org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeRegisterNewIfRequired(ObjectReferenceMapping.java:930)
	at
org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeRegisterNewIfRequired(ObjectReferenceMapping.java:916)
	at
org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeRegisterNewForCreate(ObjectBuilder.java:1945)
	at
org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4182)
	at
org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:440)
	... 1 more

-- 
View this message in context: http://old.nabble.com/Virtual-Metlhods-%7C-questions-tp34172077p34173939.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top