Search code examples
javamysqldatabasenetbeanseclipselink

How to instantiate the LAZY relationship prior to serialization?


I have just created and joined 3 new tables. Before creating them everything was working fine so I do believe the root cause is coming from their creation. Now when runned in the browser I receive this error:

org.apache.jasper.JasperException: Exception [EclipseLink-7242] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.

How does one instantiate the LAZY relationship prior to serialization anyway? I've been looking all over the web but I still haven't found real working solutions...

I also receive this other error message:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't1.rating' in 'field list' Error Code: 1054 Call: SELECT t1.id, t1.description, t1.last_update, t1.name, t1.price, t1.rating FROM category_has_product t0, product t1 WHERE ((t0.category_id = ?) AND (t1.id = t0.product_id)) bind => [1 parameter bound] Query: ReadAllQuery(name="productCollection" referenceClass=Product sql="SELECT t1.id, t1.description, t1.last_update, t1.name, t1.price, t1.rating FROM category_has_product t0, product t1 WHERE ((t0.category_id = ?) AND (t1.id = t0.product_id))")

root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 't1.rating' in 'field list'

What do all those errors mean? I don't understand because all the columns and the rows are perfectly well mapped... It says it canno't find certain columns... I have checked all my tables over and over and I really don't understand what could possibly be wrong... Everything seems fine... A little help would be very much appreciated! Thanks

Besides I use the following for the app: Netbeans Glassfish Mysql Most pages in jsp


Solution

  • Problem with instantiate lazy reference is well known, so I will provide a link rather than explain myself: http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Serialization.2C_and_Detaching

    Wiki page has also 3 solutions to solve the problem. But I will offer you another solution, which may suits your case: JSP usage.

    Check https://dzone.com/articles/open-session-view-design (or just google for 'open session in view'. Many pages refers to Hibernate but it applies to Eclipselink also). The solution is start and end transaction in servlet filter. The transaction will cover whole request processing. In your case JSP page generation will be within transaction and lazy references will just work. Benefit is that you don't have to modify your code.

    I have no idea about your second problem with mapping