Question

I have a Hibernate persist on an entity with primary key constructor, but it does not take effect in the database after something I don't know while I need it in some other methods

Here is my code:

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

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

and some where else I have :

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

where o is null. Any help? my entity code :

@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;
}

}
Was it helpful?

Solution

From Hibernate "persist" description:

However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately

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

You can use "save()" if immediately search required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top