Sunday, April 1, 2018
Tuesday, March 27, 2018
Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.internal.PersistentBag[0]
Here is part of what I found in my Eclipse console:
Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain: [here is the loop]) with root cause java.lang.StackOverflowError at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:567) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:143) at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:118) at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:24) at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:180) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:544) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:551) ...
and so on.
As the docs state (see references),
bi-directional references for ORM-managed beans (iBatis, Hibernate) would cause serialization to failure since they are cyclic dependencies. Since Jackson 1.6, this problem has been solved by the introduction of two new annotations: @JsonManagedReference and @JsonBackReference(and see the end of this post to give a look at the @JsonIdentityInfo annotation).
In computer science, in the context of data storage and transmission, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and resurrected later in the same or another computer environment.
For Jackson to work well, one of the two sides of the relationship should not be serialized, in order to avoid the annoying infinite recursive loop that causes our stackoverflow error.
So, Jackson takes the forward part of the reference, for example an attribute of a java class (i.e. List<Role> roles in User class), and converts it in a json-like storage format; this is the so-called marshalling process.
Then, Jackson looks for the back part of the reference (i.e. List<User> users in Role class) and leaves it as it is, not serializing it. This part of the relationship will be re-constructed during the deserialization (unmarshalling) of the forward reference.
Then, Jackson looks for the back part of the reference (i.e. List<User> users in Role class) and leaves it as it is, not serializing it. This part of the relationship will be re-constructed during the deserialization (unmarshalling) of the forward reference.
It's very simple. Assuming that your database query already works without JSON, all you have to do is this:
- Add the @JsonManagedReference In the forward part of the relationship (i.e. User.java class):
@Entity public class User implements java.io.Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; @Column(name="name") private String name; @ManyToMany @JoinTable(name="users_roles",joinColumns=@JoinColumn(name = "user_fk"), inverseJoinColumns=@JoinColumn(name = "role_fk")) @JsonManagedReference private Set<Role> roles = new HashSet<Role>(); ... - Add the @JsonBackReference In the back part of the relationship (i.e. Role.java class):
@Entity public class Role implements java.io.Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @ManyToMany(mappedBy="roles") @JsonBackReference private Set<User> users = new HashSet<User>(); ...
The work is done. If you take a look at your firebug logs, you'll notice that the infinite recursive loop has disappeared.
Resource Link:
How To Solve JSON infinite recursion Stackoverflow (with Spring and Jackson annotations)
Tuesday, March 20, 2018
Hibernate/JPA one-to-many and many-to-many examples
- https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
- https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
- https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/
- https://github.com/hellokoding/jpa-manytomany-extracolumns-springboot-maven-mysql
- https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
- https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-many-mapping-example/
- https://www.callicoder.com/hibernate-spring-boot-jpa-many-to-many-mapping-example/
- https://github.com/callicoder/jpa-hibernate-tutorials/blob/master/jpa-many-to-many-demo/src/main/java/com/example/jpa/model/Post.java
Tuesday, March 13, 2018
Best Cron Job Tutorials
- http://www.kswaughs.com/2016/02/spring-boot-task-scheduler-example.html
- https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html
- https://stackoverflow.com/questions/2598712/how-to-parameterize-scheduledfixeddelay-with-spring-3-0-expression-language
- https://www.codeday.top/2017/06/18/29918.html
- http://www.javainuse.com/spring/bootTask
- https://spring.io/guides/gs/scheduling-tasks/
- http://www.javarticles.com/2016/05/spring-scheduler-example.html
- http://mycuteblog.com/spring-task-scheduler-cron-job-example/
- https://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am
- https://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
- https://howtodoinjava.com/spring/spring-boot/enable-scheduling-scheduled-job-example/
Monday, March 12, 2018
Viewing New and Recent Connections and Contacts in Linkedin
You can view a list of your 1st-degree Connections on the Connections page. You can sort your connections by name or by most recently added.
To sort your connections list to show the most recently added at the top:
- Click the
My Network icon at the top of your LinkedIn homepage. - Click See all below Your connections on the left rail.
- Click the Sort by
dropdown above your connection list and select Recently added (which is usually selected by default).
You can view a list of your synced and imported contacts from the Contacts page.
To view and sort your contacts list to show the most recently added at the top:
- Click the
My Network icon at the top of your LinkedIn homepage. - Click See all below Your connections on the left rail.
- Click
Manage synced and imported contacts on the right rail. - Click the Sort by
dropdown above your connection list and select Recently added (which is usually selected by default).
Note: You can only sort by Recently added for synced contacts.
Subscribe to:
Posts (Atom)