如何处理尚未持久的实体集? Java JPA

时间:2011-10-13 04:38:18

标签: java jpa

我有两个表由JPA映射,具有一对多关系。我想将Set添加到Blog实体,但由于BlogNodes条目尚未持久化,因此它们没有Id字段,所以当我尝试将第二个元素添加到Collection时,我有nulpointer异常。我曾尝试将GeneType.TABLE用于id生成器,但它没有帮助。 Id仍为空。这是我的实体类,其中有一些字段是ometted。

Blog.java

@Entity
@Table(name = "t_blog")
public class Blog extends VersionedEntity{
(Identified id generation)
    private static final Logger log = LoggerFactory.getLogger(Blog.class);
    //@ToDo: pass actual value to serialVersionUID
    //private static final long serialVersionUID = 1882566243377237583L;

...

    @OneToMany(mappedBy = "parentBlog", fetch = FetchType.LAZY, orphanRemoval=true, cascade = { CascadeType.PERSIST, CascadeType.MERGE}) 
    private Set<BlogNode> blogNodes; 

BlogNode.java

@Entity
@Table(name = "t_blog_node")
public class BlogNode{
    /***************************************************************************************/
    @TableGenerator(name="tab", initialValue=0, allocationSize=5)
    @GeneratedValue(strategy=GenerationType.TABLE, generator="tab")
    @Column(name = "id", nullable = false, updatable = false)
    @Id
    private Long id;

    public Long getId() {
        return id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BlogNode)) return false;
        BlogNode that = (BlogNode) o;
        return that.id.equals(id);

    }

    @Override
    public int hashCode() {
        return id == null ? 0 : id.hashCode();
    }
/*************************************************************************************/

    @OneToOne
    @JoinColumn(name="parent_blog_fk", referencedColumnName="id", nullable = true)
    private Blog parentBlog; 

主要课程

public List<Blog> createBlog(int n){
    params.put("BlogName","SampleBlogName");
    params.put("BlogAlias","defaultAlias");
    params.put("BlogDescription","defaultBlog description");
    List<Blog> newBlogs = new ArrayList<Blog>();
    while(n-->0){
        Blog entry = new Blog();
        entry.setBlogName(params.get("BlogName")+n);
        entry.setBlogAlias(params.get("BlogAlias")+n);
        entry.setBlogDescription(params.get("BlogDescription")+n);
        entry = blogDAO.save(entry);
        entry.setBlogNodes(createBlogNodes(entry, NUM_OF_NODES));
        entry = blogDAO.save(entry);
        newBlogs.add(entry);
    }

    return newBlogs;
}

private Set<BlogNode> createBlogNodes(Blog blog, int numOfNodes) {
    params.put("nodeTitle","SamplenodeName");
    params.put("nodeAlias","defaultAlias");
    params.put("nodeTeaser","default node teaser");
    params.put("nodeText","default node text");
    Set<BlogNode> nodes = new HashSet<BlogNode>();;
    while (numOfNodes-->0){ 
        BlogNode node = new BlogNode();
        node.setNodeTitle(params.get("nodeTitle")+numOfNodes);
        node.setNodeAlias(params.get("nodeAlias")+numOfNodes);
        node.setNodeText(params.get("nodeText")+numOfNodes);
        node.setParentBlog(blog);
        node.setNodeTeaser(params.get("nodeTeaser")+numOfNodes);
                    //Exception raises on the second iteration 
        nodes.add(node);
    }
    return nodes;
}

我可以用另一种方式击败这个,而不是单独坚持BlogNode的单个实体吗?

1 个答案:

答案 0 :(得分:1)

您正在将节点添加到普通HashSet。导致NPE的唯一方法是它来自hashCodeequals方法。我再次指出你关于该主题的Hibernate manual。简而言之,这些方法不应仅仅因为这个原因而使用持久性ID(等等)。