I have a problem with my web project. I'm using jackson, and have problem with serialization. When i try to return a hashmap back to page this is message of the error:
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21)
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
I implemented serializable in my bean and it has empty constructor,getters and setters.
Its probably because you have a child object, and one of its elements contains the parent object, so jackson is trying to serialize the child object, creating again a serialized parent, which will cause the parent to create again a serialized child, and the child will cause a serialized parent again, so its a never ending loop for jacokson.
Related
I am working on POC of jersey REST service to be consumed by js MVC framework. On one of the forms i need to return UserProfile object (serialized to JSON by Jackson) which will be used to pre-populate HTML form. On form submission only a subset of fields must be sent to server (since some fields like "role" are read-only and must not be changed) so input JSON will be mappped to UserProfileUpdateRequest object. From server-code maintenance point of view i would like to have have a relationship between these 2 objects, since UserProfileUpdateRequest will be a subset of UserProfile, so my first choice is to use composition: UserProfile contains UserProfileUpdateRequest.
The problem is that when UserProfile is serialized to JSON by jackson, all properties of referenced UserProfileRequest instance will be wrapped in userProfileRequest field- what seems to be quite natural but is not acceptable for JS guys (or at least i was told it is not acceptable). Is there any way i could force jackson to "flat" root object and point for which referenced objects its properties must be serialized under root? A little example
class UserProfileRequest{
private String a;
private String b;
...
}
class UserProfile{
private String role;
...
private UserProfileRequest userProfileRequest;
}
So when UserProfile is serialized i got:
{"role":"admin",...,"userProfileRequest":{"a":"...","b":"...",...}}
but would like to get
{"role":"admin",...,"a":"...","b":"...",...}
I am using Jackson 1.9.7.
I think you are looking for the #JsonUnwrapped annotation.
class UserProfile{
private String role;
...
#JsonUnwrapped
private UserProfileRequest userProfileRequest;
}
Edit: Here is the link to #JsonUnwrapped in Jackson 1.9.9, so it should be available in 1.9.7, too.
When I load an entity which contains lazy-loading fields and I want to send this entity through an object message with ActiveMQ, will I receive :
The full entity (with lazy-loading fields loaded)
OR
The entity as sent (without lazy-loading fields loaded) ?
In any case do I need to put the Serializable marker in my entity ?
In case of answer 1 what do I need to do to get the entity as described in answer 2 ?
Lets assume you have an entity with some lazy loading fields like this:
#Entity
public class Foo
{
#Id
private Long id;
#OneToMany
private List<Bar> bars;
}
When you receive this entity from your database only the id field will be loaded as the bars field is lazy loaded (OneToMany is lazy by default). Now when you pass this entity through JMS for example the bars field is not initialized because the getBars() method isn't called. When the remote end calls the getter it will get a LazyInitializationException as the entity is detached and the collection isn't initialized.
If you however want the remote end to be able to call the getBars() method you need to initialize the collection in some way. You can make the fetching of the collection eager using #OneToMany(fetch = FetchType.EAGER). An alternative is to use a separate query for eager loading the entity (which is my personal preference).
Example:
select f from Foo f join fetch l.bars
If you really want to go wild and want the collection to be initialized lazily on the remote end you can take a look at Hibernate remote lazy loading using RMI. I'm not going to explain this because it seems out of the scope of the question. ;-)
I am adding a "rounding" business rule to round a decimal property value to the number of decimal places specified in a separate integer property. This works nicely if both properties are members of the business object in question. As in the following VB.Net code...
BusinessRules.AddRule(New Round(_decimalProperty, _precisionProperty))
I have a private Round class that inherits from CommonBusinessRule and its constructor is as follows:
Public Sub New(decimalProperty As IPropertyInfo, precisionProperty As IPropertyInfo)
MyBase.New(decimalProperty)
InputProperties = New List(Of IPropertyInfo)()
InputProperites.Add(decimalProperty)
InputProperties.Add(precisionProperty)
End Sub
This triggers the rule Execute whenever either property changes, and the Execute code rounds just exactly like I want.
The Problem: I now have a situation where the precisionProperty is a property of the Parent business object. When the CSLA method for adding Business Rules for the Child Business Object is called, the Parent member of the Child Business Object is null, so I can't get to a reference of the parent's property. Is there any point in time AFTER the Parent field is no longer null, that I am allowed to add a new Business Rule? If so in what method? Is there another approach?
We have looked into passing down a reference to the parent business object (via constructors) to the child, but have decided against this approach for now (the child is actually 6 levels deep, and it appears this would require rework of our code generation schemes).
The parent property in the BusinessBase is generally used by the BusinessListBase to keep a relationship with its children. The parent property really isn't(shouldn't be?) used outside of that.
When I have a parent object with a property that affects child objects, I put the rule in the parent object that will then invoke any rules on the child object. You can pass in any values you like, even to the point of having a copy of the property on the child and just setting that as the parent property changes.
Im pretty new to nhibernate so this may be quite straightforward but i havent found an answer on the web yet.
Lets say i have a Parent class and a Child class. The Parent Class can have many Child classes associated with it. Now when i try to load a specific Parent nhibernate also populates its Child collection for me. There are situations where I want to just return a Parent class without a Child collection.
I know i can turn on Lazy loading but that wont work as im serializing the Parent to XML. The XML serialiser cannot work with the nhibernate PersistanceBag that contains the Child collection.
So is there a way to define a Parent class, lets say ParentView which works on the same table but only contains the Parent properties and not all its children and grandchildren?
Define a class ParentView that contains the columns you need to retrieve. Make sure this class has one parameterless constructor.
ISession session = NHibernateHelper.Session;
ISQLQuery query = session.CreateSQLQuery("select Parent_ID, Name form Parent where Parent_ID = :parentID");
query.SetInt32("parentID", parentID);
IList<ParentView> parentView = query.SetResultTransformer(Transformers.AliasToBean<ParentView>()).List<ParentView>();
return parentView;
An alternative to creating a view class and associated query as suggested by sh_kamalh (which I would consider if I were you). If the problem is related to the bag mapping structure specifically then you might have a couple of easier solutions:
Option 1
Revisit the bag mapping - Maybe simple selecting a different strategy will fix the issue. I have answered a question on the different collection mappings before List vs Set vs Bag in NHibernate personally I find that I use the Set strategy a lot. To map a different strategy in Fluent NHibernate use the following as a guide in your override.
mapping.HasMany<Child>(x => x.Children).ToSet();
or
mapping.HasMany<Child>(x => x.Children).ToList();
Option 2
Not particularly related to NHibernate but if you are using the default xml serializer you might be able to tell the xml serializer to simply ignore that property and leave the bag mapping in place.
[System.Xml.Serialization.XmlIgnore]
public IEnumerable<Child> Children { get; internal set; }
I'm creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class.
The following code causes a run-time error MOVE_CAST_ERROR:
data: o_child type ref to zchild.
o_child ?= zparent=>provide_instance( ).
I have also tried:
data: o_parent type ref to zparent,
o_child type ref to zchild.
o_parent = zparent=>provide_instance( ).
o_child ?= o_parent.
I have succesfully down-casted with other object types in the past - does anyone have an idea what to look for in ZPARENT or ZCHILD that may make the two classes incompatible?
Currently ZCHILD only has one extra method, which if added to the super class would break the abstraction of the class, so I'm not that keen to do it.
I didn't do any object oriented coding in abap. Nor much abap programming. But I think that this is a typical oo scenario. So here is my guess.
You can't cast an object to what it ain't.
You have created an instance of the parent class. Now the child class has the property of "myCandy" that the parent class don't have. So your object don't have this property. And then you cast it to child. What the poor run time has to do when you ask for ( or change ) the "myCandy" property? It can do nothing. So it disallow this cast.
The cast is only possible if the object was instantiated as the child and then it was casted to the parent object and then back again to the child object. The child has everything the parent has so there is no problem with this path.