When I declare an association in a mapper, I expect MyBatis to use the setter to set a new instance for this property.
What happens: MyBatis uses the getter (in the following example: getItem()) to get the value before it is set.
<association property="item" javaType="Element">
...
</association>
How can I tell Mybatis to create a new instance to set instead of using the one it gets with the getter.
Related
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.
I want mybatis to call a factory method to create an object instead of a constructor. So that for null valued attributes i can return a NULL object(which has overridden behavior to handle all the edge cases) instead of actual object. Can i achieve that with mapper.xml?
Define your own ObjectFactory
http://www.mybatis.org/core/configuration.html#objectFactory
To answer your specific question, there is no way to specify a factory method directly (and only) in the mapper.xml file itself, as far as I know. However, there are two options in MyBatis to do what you want:
As stated in Bhaskar's answer you can use an ObjectFactory.
In theory, you can also define a TypeHandler, but I was unable to get this to work in my recent testing.
If you would like to see a working example of how to use a MyBatis ObjectFactory to implement a Null object, see koan19 of my MyBatis koans: https://github.com/midpeter444/mybatis-koans. (Look in the completed-koans/koan19 directory for the solution I came up with.)
I am very new to ADo.net entity framework and i am getting the following error
The type 'Edm.Byte' of the member 'IsActive' in the conceptual side type
NopSolutions.NopCommerce.BusinessLogic.Data.Language' does not match with the type
System.Boolean' of the member 'IsActive' on the object side type NopSolutions.NopCommerce.BusinessLogic.Directory.Language'.
as far i understand that there is some missing to relating the data type of edm and object
but i made a column in database table which is bit type and in langauge.cs i declare the property of
public bool IsActive { get; set; }
any details needed i can post here
EDIT :
as i google around i found this question on stackoverflow
et-model-property-to-boolean-in-entity-framework which is changing the Byte to Boolean for mapping tinyint
but in my case i have in database is also the bit.
Assuming that you have an .edmx-file you can modify. Open it in an XML-editor and search for the <edmx:ConceptualModels>-element. This should contain the definition of your entities, look for the <EntityType Name="Language">-element, which should declare something like this:
<Property Name="IsActive" Type="Byte" Nullable="false" />
Change the Type-attribute to Boolean.
I'm pretty sure that you could also do this in the designer, but if you don't have direct access to the .edmx let me know and we figure something out.
I've got the following config <when-property-is name="isExecute" value="true" />.
Am I able to set property isExecute for module A true and for module B false?
Am I able to change property value by Java?
Does GWT.create read property condition each call or only once (cache)?
What are you trying to achieve? Replacing implementations during runtime? You can use Generator to do this.
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.