我有一对多关系,这是我的代码
@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 }
我得到了例外:
org.springframework.http.converter.HttpMessageNotWritableException:无法编写内容:无法延迟初始化角色集合:com.example.helios.model.Catalog.items,无法初始化代理- 没有会话;嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法延迟初始化角色集合:com.example.helios.model.Catalog.items,无法初始化代理- 没有会话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
我的版本是:
这是正常的hibernate行为
在一对多关系中,hibernate会以LAZY模式加载父实体(在您的情况下为目录),但会加载子实体列表(在您的情况下为列表项和列表顺序)
这意味着您无法访问这些对象,因为它们只是代理,而不是真实的对象
为了避免在执行查询时加载整个数据库,这很有用
您有2个解决方案: