I'm working on a project using Hibernate and Jackson to serialize my objects.
I think I understand how it is suposed to work but I can't manage to make it works.
If I understand well, as soon as a relation fetch mode is set to LAZY, if you want this relation, you have to initialize it.
Here is my class :
#Entity
#JsonIgnoreProperties(ignoreUnknown = true)
#Table(schema="MDDI_ADMIN", name = "MINIUSINE")
#Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class MiniUsine {
#Id
#Column(name="MINIUSINEID", nullable = false)
private int miniUsineID;
#Column(name = "NAME", length = 40, nullable = false)
private String name;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="FluxID")
#JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<Flux> fluxs = new HashSet<Flux>();
And all getters and setters.
I've also tried this JsonInclude.Include.NON_EMPTY as class annotation. Also tried the NON_NULL.
However, jackson keeps sending me
com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: MiniUsine.fluxs, no session or session was closed (through reference chain: java.util.ArrayList[0]->MiniUsine["fluxs"])
I'm serializing it with : mapper.writeValueAsString(optMU);
Using Jackson 2.3.2
Thanks for help
I know this is an old question but I had the same problem.
You must add a new maven dependecy to support JSON serialization and deserialization of Hibernate. I used Hibernate5 so I added
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.9.2</version>
</dependency>
Now register the new module.
#Provider
public class JacksonHibernateProvider implements ContextResolver<ObjectMapper> {
#Override
public ObjectMapper getContext(final Class<?> type) {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate5Module());
return mapper;
}
}
As far as I understand, the entity object that hibernate returns is a proxy which derives from your entity class. If you try to access getter methods for lazy fields outside of a transaction, you get LazyInitializationException.
The point I want to make is setting fluxs to empty set doesn't help you at all.
private Set<Flux> fluxs = new HashSet<Flux>();
Hibernate overloads the getter and if you try to access it outside of a transaction(which jackson is doing to check if it is empty), you get the LazyInit error.
I have two entities: Order and Item in a OneToMany relationship. Item belongs an Order, and the Order has a set of Items.
class Order{
#OneToMany(fetch = FetchType.EAGER, mappedBy = "id_order")
Set<Item> items;
}
class Item{
#ManyToOne(targetEntity = Order.class, fetch = FetchType.LAZY)
#JoinColumn(name = "id_order")
Order id_order;
}
I use gson to serialize Orders and send them to another machine, but a loop is being created during serialization, due to both orders and items having a reference to each other.
My goal is that when an Item is loaded, the field id_order should either be null or contain only the id, to avoid propagation. Does hibernate support this feature? Or can I exclude the field during serialization?
I already tried FetchType.LAZY on Item and catching Item inside an onLoad() Interceptor and setting its id_order to null. But it didn't work. I am trying to avoid writing a custom adapter or manually parsing all Items inside all Orders at every query.
I believe it has not much with Hibernate, but more with GSON. You should define serialization/deserialization rule.
Easiest way is to use #Expose annotation, to include/exclude given property from serialization/deserialization:
#Expose(serialize = false, deserialize = false)
Another way is to define custom adapters for givem class. WIth Adapter you can completely override the way GSON serialize or deserialize object.
For example:
public class YourAdapter implements JsonDeserializer<Order>, JsonSerializer<Order> {
#Override
public Orderde serialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
// your logic
}
#Override
public JsonElement serialize(Ordersrc, Type typeOfSrc, JsonSerializationContext context) {
// your logic
}
}
Finally initialize instance of your Gson parser:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Order.class, new YourAdapter())
.build();
If you want to avoid writting whole serialization, you could exclude your field by using #Expose, and then write adapter with pure instance of GSON in it, serialize/deserialize using that pure one and manually add that one field.
Another way of dealing with serialization/deserialization problem is to use ExclusionStrategy described here: https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/ExclusionStrategy.html
I know this doesnt answer the question derectly but might help others, GSON has an option to exclude based on a vairiables modifier.
Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE).create();
I want to use ORMLite to query data from SQLite and store it in Java class, then convert this class to JSON using Jackson JSON library and send it through HTTP. I also want to do opposite - get data from server in JSON and convert it to Java class and save this class to SQLite using ORMLite.
Can I do this using one class per table for both ORMLite and Jackson?
Yes you can, why not? you can convert to json any java object you want
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);;
ORMLite create entity, which is java object so.
#DatabaseTable(tableName = "accounts")
public class Account {
#DatabaseField(id = true)
private String name;
#DatabaseField(canBeNull = false)
private String password;
...
Account() {
// all persisted classes must define a no-arg constructor with at least package visibility
}
...
}
yes you can.
I am writing a serializer to serialize POJO to JSON but stuck in circular reference problem. In hibernate bidirectional one-to-many relation, parent references child and child references back to parent and here my serializer dies. (see example code below)
How to break this cycle? Can we get owner tree of an object to see whether object itself exists somewhere in its own owner hierarchy? Any other way to find if the reference is going to be circular? or any other idea to resolve this problem?
I rely on Google JSON To handle this kind of issue by using The feature
Excluding Fields From Serialization and Deserialization
Suppose a bi-directional relationship between A and B class as follows
public class A implements Serializable {
private B b;
}
And B
public class B implements Serializable {
private A a;
}
Now use GsonBuilder To get a custom Gson object as follows (Notice setExclusionStrategies method)
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
public boolean shouldSkipClass(Class<?> clazz) {
return (clazz == B.class);
}
/**
* Custom field exclusion goes here
*/
public boolean shouldSkipField(FieldAttributes f) {
return false;
}
})
/**
* Use serializeNulls method if you want To serialize null values
* By default, Gson does not serialize null values
*/
.serializeNulls()
.create();
Now our circular reference
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
String json = gson.toJson(a);
System.out.println(json);
Take a look at GsonBuilder class
Jackson 1.6 (released september 2010) has specific annotation-based support for handling such parent/child linkage, see http://wiki.fasterxml.com/JacksonFeatureBiDirReferences. (Wayback Snapshot)
You can of course already exclude serialization of parent link already using most JSON processing packages (jackson, gson and flex-json at least support it), but the real trick is in how to deserialize it back (re-create parent link), not just handle serialization side. Although sounds like for now just exclusion might work for you.
EDIT (April 2012): Jackson 2.0 now supports true identity references (Wayback Snapshot), so you can solve it this way also.
Can a bi-directional relationship even be represented in JSON? Some data formats are not good fits for some types of data modelling.
One method for dealing with cycles when dealing with traversing object graphs is to keep track of which objects you've seen so far (using identity comparisons), to prevent yourself from traversing down an infinite cycle.
In addressing this problem, I took the following approach (standardizing the process across my application, making the code clear and reusable):
Create an annotation class to be used on fields you'd like excluded
Define a class which implements Google's ExclusionStrategy interface
Create a simple method to generate the GSON object using the GsonBuilder (similar to Arthur's explanation)
Annotate the fields to be excluded as needed
Apply the serialization rules to your com.google.gson.Gson object
Serialize your object
Here's the code:
1)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.METHOD})
public #interface GsonExclude {
}
2)
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public class GsonExclusionStrategy implements ExclusionStrategy{
private final Class<?> typeToExclude;
public GsonExclusionStrategy(Class<?> clazz){
this.typeToExclude = clazz;
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return ( this.typeToExclude != null && this.typeToExclude == clazz )
|| clazz.getAnnotation(GsonExclude.class) != null;
}
#Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(GsonExclude.class) != null;
}
}
3)
static Gson createGsonFromBuilder( ExclusionStrategy exs ){
GsonBuilder gsonbuilder = new GsonBuilder();
gsonbuilder.setExclusionStrategies(exs);
return gsonbuilder.serializeNulls().create();
}
4)
public class MyObjectToBeSerialized implements Serializable{
private static final long serialVersionID = 123L;
Integer serializeThis;
String serializeThisToo;
Date optionalSerialize;
#GsonExclude
#ManyToOne(fetch=FetchType.LAZY, optional=false)
#JoinColumn(name="refobj_id", insertable=false, updatable=false, nullable=false)
private MyObjectThatGetsCircular dontSerializeMe;
...GETTERS AND SETTERS...
}
5)
In the first case, null is supplied to the constructor, you can specify another class to be excluded - both options are added below
Gson gsonObj = createGsonFromBuilder( new GsonExclusionStrategy(null) );
Gson _gsonObj = createGsonFromBuilder( new GsonExclusionStrategy(Date.class) );
6)
MyObjectToBeSerialized _myobject = someMethodThatGetsMyObject();
String jsonRepresentation = gsonObj.toJson(_myobject);
or, to exclude the Date object
String jsonRepresentation = _gsonObj.toJson(_myobject);
If you are using Jackon to serialize, just apply #JsonBackReference to your bi-directinal mapping
It will solve the circular reference problem.
Note : #JsonBackReference is used to solve the Infinite recursion (StackOverflowError)
Used a solution similar to Arthur's but instead of setExclusionStrategies I used
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
and used #Expose gson annotation for fields which I need in the json, other fields are excluded.
if you are using spring boot,Jackson throws error while creating response from circular/bidirectional data, so use
#JsonIgnoreProperties
to ignore circularity
At Parent:
#OneToMany(mappedBy="dbApp")
#JsonIgnoreProperties("dbApp")
private Set<DBQuery> queries;
At child:
#ManyToOne
#JoinColumn(name = "db_app_id")
#JsonIgnoreProperties("queries")
private DBApp dbApp;
If you are using Javascript, there's a very easy solution to that using the replacer parameter of JSON.stringify() method where you can pass a function to modify the default serialization behavior.
Here's how you can use it. Consider the below example with 4 nodes in a cyclic graph.
// node constructor
function Node(key, value) {
this.name = key;
this.value = value;
this.next = null;
}
//create some nodes
var n1 = new Node("A", 1);
var n2 = new Node("B", 2);
var n3 = new Node("C", 3);
var n4 = new Node("D", 4);
// setup some cyclic references
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n1;
function normalStringify(jsonObject) {
// this will generate an error when trying to serialize
// an object with cyclic references
console.log(JSON.stringify(jsonObject));
}
function cyclicStringify(jsonObject) {
// this will successfully serialize objects with cyclic
// references by supplying #name for an object already
// serialized instead of passing the actual object again,
// thus breaking the vicious circle :)
var alreadyVisited = [];
var serializedData = JSON.stringify(jsonObject, function(key, value) {
if (typeof value == "object") {
if (alreadyVisited.indexOf(value.name) >= 0) {
// do something other that putting the reference, like
// putting some name that you can use to build the
// reference again later, for eg.
return "#" + value.name;
}
alreadyVisited.push(value.name);
}
return value;
});
console.log(serializedData);
}
Later, you can easily recreate the actual object with the cyclic references by parsing the serialized data and modifying the next property to point to the actual object if it's using a named reference with a # like in this example.
This is how i finally solved it in my case. This works at least with Gson & Jackson.
private static final Gson gson = buildGson();
private static Gson buildGson() {
return new GsonBuilder().addSerializationExclusionStrategy( getExclusionStrategy() ).create();
}
private static ExclusionStrategy getExclusionStrategy() {
ExclusionStrategy exlStrategy = new ExclusionStrategy() {
#Override
public boolean shouldSkipField(FieldAttributes fas) {
return ( null != fas.getAnnotation(ManyToOne.class) );
}
#Override
public boolean shouldSkipClass(Class<?> classO) {
return ( null != classO.getAnnotation(ManyToOne.class) );
}
};
return exlStrategy;
}
Jackson provides JsonIdentityInfo annotation to prevent circular references. You can check the tutorial here.
This error can appened when you have two objects :
class object1{
private object2 o2;
}
class object2{
private object1 o1;
}
With using GSon for serialization, i have got this error :
java.lang.IllegalStateException: circular reference error
Offending field: o1
To solved this, just add key word transient :
class object1{
private object2 o2;
}
class object2{
transient private object1 o1;
}
As you can see here : Why does Java have transient fields?
The transient keyword in Java is used to indicate that a field should not be serialized.
If you use GSON to convert Java class in JSON you can avoid the fields that cause the circular reference and the infinitive loop, you only have to put the annotation #Expose in the fields that you want to appear in the JSON, and the fields without the annotation #Expose do not appear in the JSON.
The circular reference appears for example if we try to serialize the class User with the field routes of class Route, and the class Route have the field user of the class User, then GSON try to serialize the class User and when try to serialize routes, serialize the class Route and in the class Route try to serialize the field user, and again try to serialize the class User, there is a circular reference that provoke the infinitive loop. I show the class User and Route that mentioned.
import com.google.gson.annotations.Expose;
Class User
#Entity
#Table(name = "user")
public class User {
#Column(name = "name", nullable = false)
#Expose
private String name;
#OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
#OnDelete(action = OnDeleteAction.CASCADE)
private Set<Route> routes;
#ManyToMany(fetch = FetchType.EAGER)
#OnDelete(action = OnDeleteAction.CASCADE)
#JoinTable(name = "like_", joinColumns = #JoinColumn(name = "id_user"),
inverseJoinColumns = #JoinColumn(name = "id_route"),
foreignKey = #ForeignKey(name = ""),
inverseForeignKey = #ForeignKey(name = ""))
private Set<Route> likes;
Class Route
#Entity
#Table(name = "route")
public class Route {
#ManyToOne()
#JoinColumn(nullable = false, name = "id_user", foreignKey =
#ForeignKey(name = "c"))
private User user;
To avoid the infinitive loop, we use the annotation #Expose that offer GSON.
I show in format JSON the result of serialize with GSON the class User.
{
"name": "ignacio"
}
We can see that the field route and likes do not exist in the format JSON, only the field name. Because of this, the circular reference is avoid.
If we want to use that, we have to create an object GSON on a specific way.
Gson converterJavaToJson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
In the end, we transform the java class of the model of hibernate user using the conversor GSON created.
User user = createUserWithHibernate();
String json = converterJavaToJson.toJson(user);
the answer number 8 is the better, i think so if you know what field is throwing a error the you only set the fild in null and solved.
List<RequestMessage> requestMessages = lazyLoadPaginated(first, pageSize, sortField, sortOrder, filters, joinWith);
for (RequestMessage requestMessage : requestMessages) {
Hibernate.initialize(requestMessage.getService());
Hibernate.initialize(requestMessage.getService().getGroupService());
Hibernate.initialize(requestMessage.getRequestMessageProfessionals());
for (RequestMessageProfessional rmp : requestMessage.getRequestMessageProfessionals()) {
Hibernate.initialize(rmp.getProfessional());
rmp.setRequestMessage(null); // **
}
}
To make the code readable a big comment is moved from the comment // ** to below.
java.lang.StackOverflowError [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain: com.service.pegazo.bo.RequestMessageProfessional["requestMessage"]->com.service.pegazo.bo.RequestMessage["requestMessageProfessionals"]
For example, ProductBean has got serialBean. The mapping would be bi-directional relationship. If we now try to use gson.toJson(), it will end up with circular reference. In order to avoid that problem, you can follow these steps:
Retrieve the results from datasource.
Iterate the list and make sure the serialBean is not null, and then
Set productBean.serialBean.productBean = null;
Then try to use gson.toJson();
That should solve the problem