EntityManager在持久化后找不到任何内容

时间:2014-02-04 10:44:03

标签: java hibernate transactions persist

我有一个Hibernate在具有主键构造函数的实体上持久存在,但是在我不知道的事情之后它在数据库中没有生效,而在其他方法中我需要它

这是我的代码:

Base_Document document = new Base_Document(workItem.getProcessInstanceId());

this.getEntityManager().persist(document);
this.getEntityManager().flush();
this.getEntityManager().close();

以及我拥有的其他地方:

Object o = this.getEntityManager().find(Base_Document.class,
    workItem.getProcessInstanceId());

其中onull。有帮助吗? 我的实体代码:

@Entity
public class Base_Document implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long processInstanceId;

private DocumentStatus status;

@ManyToOne
private Base_Roles creatorRole;

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date createDate;

public Base_Document() {}

public Base_Document(Long processInstanceId) {
    this.processInstanceId = processInstanceId;
    this.createDate = new Date();
    this.status = DocumentStatus.DRAFT;
    try {
        String currentUser =  PublicUtils.getHttpServletRequest().getUserPrincipal().getName();
        this.setCreatorRole(PublicUtils.getEntityManager().find(Base_Roles.class, currentUser));
    } catch (PolicyContextException ex) {
        Logger.getLogger(Base_Document.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public Long getId() {
    return processInstanceId;
}

public void setId(Long id) {
    this.processInstanceId = id;
}

public Long getProcessInstanceId() {
    return processInstanceId;
}

public void setProcessInstanceId(Long processInstanceId) {
    this.processInstanceId = processInstanceId;
}

public DocumentStatus getStatus() {
    return status;
}

public void setStatus(DocumentStatus status) {
    this.status = status;
}

public Base_Roles getCreatorRole() {
    return creatorRole;
}

public void setCreatorRole(Base_Roles creatorRole) {
    this.creatorRole = creatorRole;
}

public Date getCreateDate() {
    return createDate;
}

public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}

}

1 个答案:

答案 0 :(得分:0)

来自Hibernate“坚持”描述:

  

但是,它不保证标识符值将立即分配给持久性实例

https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/Hibernate_Core_Reference_Guide/objectstate-makingpersistent.html

如果需要立即搜索,您可以使用“save()”。