How to convert Mybatis Javassist proxies object to source object(unproxy object)? - mybatis

Mybatis excute sql and return a Javassist proxies object.
I want to convert Javassist proxies object to source object.
Since sometimes I don't want to pass the proxy object in my code(It will cause association SQL when other framework reflect the object). I want to use the source object(with no proxy).
for example:
select * from user where id = 1;
Mybatis return an Object type : User$$_jvstec7_0#a5843b
But I want to convert it to unproxy object: User#c5641a
How can I get an unproxy object without set every member field in object?
Thanks in advance.

Related

Assign non persisted object to view using extbase

While trying to assign an object to a view in my controller action I get the following message because this object is not persisted:
Could not serialize Domain Object Vendor\Extension\Domain\Model\Object. It is neither an Entity with identity properties set, nor a Value Object.
Is there any possibility to add this object to the view without creating a databaseentry?
The exception [InvalidArgumentValueException('Could not serialize Domain Object $className. It is neither an Entity with identity properties set, nor a Value Object.', 1260881688)][1] is thrown in the UriBuilder, thus when a model shall be used as argument for creating a link.
The instance of Vendor\Extension\Domain\Model\Object must either fulfill these requirements:
can be represented as array (is array or implements Iterator interface) OR
extends TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject AND one of
extends TYPO3\CMS\Extbase\DomainObject\AbstractValueObject OR
having a valid uid, not null
Thus, if you instantiated the object directly in the controller, the uid property is not defined yet. This property is assigned if domain objects are fetched or added with a repository.
TypeConverters
TypeConverters allow to convert from a given identifier (some string representation, hash-value, ...) to a proper domain object. The following links show how to do that for the concept of an IBAN (International Bank Account Identifier).
IBAN model
TypeConverter to create object from string
TypeConverters have to registerd in ext_localconf.php like this:
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter(
\H4ck3r31\BankAccountExample\Domain\Property\TypeConverter\IbanTypeConverter::class
);
The Iban object can be used then in your controller:
public function someAction(Iban $iban) { ... }
Use array representation of your object
Another alternative could be to assign an array representation of the domain object to the view and use that to fill the link arguments:
$this->view->assign('myObject', $object->toArray());
When invoking a controller action, the object is reconstituted from the submitted array keys and are used as properties - thus array keys and properties must have the same naming, or a persistence column mapping is defined.
public function someAction(MyObject $object) { ... }
In my previously mentioned bank account example it looks like this:
BankDto model
Controller action invocation
The term "Dto" is the abbreviation for "Data Transfer Object", thus it's not a real domain entity, does not have a proper UID and is just used to encapsulate information in a domain object when passing that to different components.

Doctrine - mapping an entity from an external library

i'm using an external library (the awesome nicmart/Tree to build trees) that returns me an object that is the extension of the original object produced by the library
class originalObject
{
//some properties
// this is the object produced by the library
// i dont want to modify the external library so no mapping here
}
class myObject extends originalObject
{
//this is the entity i want to persist
// but it hasnt got any property ??
}
i want to persist myObject with Doctrine\MongoDB, therefore i need to map it.
i (obviously) dont want to modify the library itself so my question is:
where do i put the mapping ?
I thought i could override the properties (like i would do with methods) by re-declaring them and adding the mapping to the re-declaration, but they are not overriden but duplicated.
IMHO you can't do it this way. You'll have to modify originalObject (add annotations there) or you'll have to declare whole myObject and can't inherit origObj.

Unmarshalling saml metadata extensions using opensaml

I have created some extensions in saml metadata. I'm trying to unmarshall the xml using opensaml2. I have created the interface, implementation class, builder, marshaller and unmarshaller of the extension. Then I registered the object providers using Configuration.registerObjectProvider
Configuration.registerObjectProvider(RequestedAudiences.TYPE_NAME, new RequestedAudiencesBuilder(), new RequestedAudiencesMarshaller(), new RequestedAudiencesUnmarshaller());
When I try to get the extensions using the bellow code segment
List<XMLObject> extensions = spssoDescriptor.getExtensions().getUnknownXMLObjects();
It returns objects of the type
org.opensaml.xml.schema.impl.XSAnyImpl
So now I can't read any value from the object. I want to get an object of the actual extension implementation class I have created.
Can anyone suggest what I am doing wrong?
The problem was I have registered the object providers after creating the metadata object. So at the time of creating the metadata object, opensaml does not know how to create the required extension object (RequestedAudiences object). Registering the object providers before creating the metadata object resolved the problem.
You can use below scala code to extract the information.
val dato = descriptor.getExtensions().getUnknownXMLObjects.get(0).asInstanceOf[XSAny]
println(dato.getTextContent)

MagicalRecord: How to import values into a related entity

I'm using a web service which returns results like the following example:
{
"name":"Frank",
"meals":[
"cheeseburger",
"lasagne"
]
}
My Core Data schema looks like this:
Using MagicalRecord's MR_importValuesForKeysWithObject method, how would I set about mapping the meals key to the related Meals.name attribute?
I can map the meals manually after, using a for in loop, but just wondered if there was a way MR_importValuesForKeysWithObject would perform this for me?
Basically I want each object in the JSON "meals" array to become a new Meals entity.
Override importMeals: on the Person object and do the lookup/create/associate manually.
(longer answer)
every property imported via MagicalRecord calls import(PropertyName) on the target object, by implementing it you can override functionality.

What is the "Func<object> modelAccessor" parameter for in MVC's DataAnnotationsModelMetadataProvider?

It's one of the parameters supplied to the CreateMetadata method (which you override if extending metadata support).
ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor, <<--THIS ONE
Type modelType,
string propertyName)
I had assumed that it allowed you to access the model object itself (e.g. for setting metadata based on model values), however when I try to use it to cast to my model object I just get null.
Entity ent = (Entity)modelAccessor(); // = Null
If I've missunderstood, can anyone explain what it's purpose is? Or alternatively, how to properly use it?
Thanks
We originally had that as "object model", rather than "Func modelAccessor". We had to change it late in MVC 2's ship cycle.
The purpose is to delay retrieving the actual value of the model until such point as you know you're going to need it (that is, until you call ModelMetadata.Model).
The problem it solves is actually a rather esoteric one related to model binding against a LINQ to SQL class that has a foreign key reference in it. The problem is, if you've retrieved the child object which is represented by a foreign key relationship (which usually means a delay load of that object), then you're no longer allowed to choose a new child object by setting the foreign key ID property. It's very common to model bind the foreign key ID (and not the whole foreign key entity) when model binding, but if we'd retrieved the foreign key entity object (for the purposes of populating the ModelMetadata class) then that binding would no longer be legal, and actually throw an exception. Since ModelMetadata is used for both directions of models -- inbound, via model binding, and outbound, via HTML generation -- we needed to introduce the layer of indirection to protect your ability to use it in both scenarios without disrupting LINQ to SQL's rules.
The modelAccessor parameter does not point to an instance of the object, but rather it is a function that will access some attribute of your object. The Func "encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter." For example, if we have following class:
public class Bar(){
[DisplayName("I am Foo.")]
public string Foo{get;}
}
When the CreateMetaData is called, it will be to create meta data for the Foo property and the modelAccessor will be a function that returns the value of Foo.
I did a little digging and found a way to get to the instance of the object, but it requires using reflection. You can do the following to get the Bar class in my example:
if (modelAccessor != null)
{
//Use reflection to get the private field that holds the Bar object.
FieldInfo container = modelAccessor.Target.GetType().GetField("container");
//Invoke field on the modelAccessor target to get the instance of the Bar object.
Bar myObject = (Bar)container.GetValue(modelAccessor.Target);
}
I've only run this against a simple test case, so your mileage may vary, but hopefully this will help clarify what is going on.