I have One-To-Many relationship, here is my code
@Entity
@Table(name = "catalog")
public class Catalog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "catalog_id")
private int catalog_id;
@NotEmpty
@Size(min = 3, max = 255)
@Column(name = "name", nullable = false)
private String name;
@OneToMany(mappedBy="mycatalogorder")
private List<Order> orders;
@OneToMany(mappedBy="mycatalog")
private List<CatalogItem> items;
// setters and getters
}
@Entity
@Table(name = "catalogitem")
public class CatalogItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "catalogitem_id")
private int catalogitem_id;
@NotEmpty
@Size(min = 3, max = 255)
@Column(name = "name", nullable = false)
private String name;
@NotEmpty
@Column(name = "price", nullable = false)
private Double price;
@OneToOne(mappedBy="ordercatalogitem", cascade=CascadeType.ALL)
private OrderItem morderitem;
@ManyToOne
@JoinColumn(name="catalog_id", nullable=false)
private Catalog mycatalog;
// setters and getters
}
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id")
private int order_id;
@NotEmpty
@Size(min = 3, max = 255)
@Column(name = "name", nullable = false)
private String name;
@NotEmpty
@Size(min = 3, max = 1024)
@Column(name = "note", nullable = false)
private String note;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "ddmmYYYY HH:mm:ss")
@Column(name = "created", nullable = false)
private Date created;
@OneToMany(mappedBy="myorder")
private Set<OrderItem> orderItems;
@ManyToOne
@JoinColumn(name="catalog_id", nullable=false)
private Catalog mycatalogorder;
@PrePersist
protected void onCreate() {
created = new Date();
}
// setters and getters
}
@Entity
@Table(name = "orderitem")
public class OrderItem {
@Id
@Column(name="catalogitem_id", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="catalogitem"))
private int catalogitem_id;
@Column(name = "quantity")
private int quantity;
@OneToOne
@PrimaryKeyJoinColumn
private CatalogItem ordercatalogitem;
@ManyToOne
@JoinColumn(name="order_id", nullable=false)
private Order myorder;
// setters and getters
}
And I am getting the exception:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.example.helios.model.Catalog.items, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.example.helios.model.Catalog.items, could not initialize proxy - no Session org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:271) org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100) org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:222) org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183) org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
My versions is:
This is the normal Hibernate behaviour
In one to many relations, hibernate loads the father entity (Catalog in your case) but it will load the children entities List (List items and List orders in your case) in a LAZY mode
This means you can't access to these objects because they are just proxies and not real objects
This is usefull in order to avoid to load the full DB when you execute a query
You have 2 solution:
Angelo
A third option which can be useful if you don't want to use EAGER mode and load up everything is to use Hibernate::initialize and only load what you need.
Session session = sessionFactory.openSession();
Catalog catalog = (Catalog) session.load(Catalog.class, catalogId);
Hibernate.initialize(shelf);
More information
I had the same problem but a fixed by:
@OneToMany
@JoinColumn(name = "assigned_ingredient", referencedColumnName = "ingredient_id")
@Fetch(FetchMode.JOIN) // Changing the fetch profile you can solve the problem
@Where(clause = "active_ind = 'Y'")
@OrderBy(clause = "meal_id ASC")
private List<Well> ingredients;
you can have more information here: https://vladmihalcea.com/the-best-way-to-handle-the-lazyinitializationexception/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With