编组错误:未能懒惰地初始化角色集合(...),没有关闭会话或会话

时间:2013-05-03 07:39:24

标签: web-services hibernate jpa ejb

我正在使用Eclipse RCP,其中EJB暴露为WebServices。我的服务bean看起来如下

@Override
@SuppressWarnings("unchecked")
public List<Author> listAll() {
    final Query query = this.entityManager.createQuery("from Author");
    final List<Author> authors = query.getResultList();
    return authors;
}

这两个实体是:

@Entity
@Table(name = "books")
public class Book implements Serializable {

@Id
private int id;

private String title;

private String summary;

private String isbn;

@Column(name = "published")
private Date publishingDate;

@Column(name = "created")
private Date createdAt;

@Column(name = "updated")
private Date modifiedAt;

@ManyToMany
@JoinTable(
        name = "book_authors",
        joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
private List<Author> authors;

@Entity
@Table(name = "authors")
public class Author implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int id;

@Column(name = "first_name")
private String firstName;

@Column(name = "middle_name")
private String middleName;

@Column(name = "last_name")
private String lastName;

@Column(name = "created")
private Date createdAt;

@Column(name = "updated")
private Date modifiedAt;

@ManyToMany
@JoinTable(
        name = "book_authors",
        joinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"))
private List<Book> books;

在我的TestWSCLient中,我试图做这样的事情

AuthorServiceBeanServiceLocator locator = new AuthorServiceBeanServiceLocator();

    AuthorServiceBean service = locator.getAuthorServiceBeanPort();

    Author[] authors = service.listAll();

    for(Author a : authors){
        System.out.println(a.getFirstName());
    }

我收到此错误

  

{.... // xml.apache.org/axis/}stackTrace:Marshalling错误:懒得初始化角色集合:ro.cgs.entities.Author.books,没有关闭会话或会话< / p>

我提到如果我不建立多对多关系,那么一切正常。任何人都可以告诉我该怎么做才能解决这个问题? 我使用Hibernate进行持久化。 感谢。

0 个答案:

没有答案