I went through and removed all the "_" from the private instance fields... I didn't think it would matter because hibernate has always used the property name from the getter and not the field name. Re-running the test with the field names matching the accessors didnt change the test outcome in any way.
Here is the updated class:
Code:
@Entity
@Table(name = "category", schema="loftware")
public final class DocumentCategoryModel extends AbstractAclIdentityObject<DocumentCategory,DocumentCategory> implements DocumentCategory {
private static final long serialVersionUID = -3045174825695348190L;
private DocumentCategory parent;
private Set<DocumentCategory> childCategories = new HashSet<DocumentCategory>();
private Set<Document> childDocuments = new HashSet<Document>();
private Long id;
private SortedSet<DocumentCategoryAclEntry> aclEntries = new TreeSet<DocumentCategoryAclEntry>();
private static int allPermissionsValue = LoftwarePermission.READ.getMask() +
LoftwarePermission.WRITE.getMask() +
LoftwarePermission.CREATE.getMask() +
LoftwarePermission.DELETE.getMask() +
LoftwarePermission.ADMINISTRATION.getMask() +
LoftwarePermission.APPROVE.getMask() +
LoftwarePermission.PUBLISH.getMask() +
LoftwarePermission.PRINT.getMask() +
LoftwarePermission.LIST.getMask();
@Override
@GeneratedValue(strategy = GenerationType.AUTO, generator="DocCatModelSeq")
@SequenceGenerator(name="DocCatModelSeq",sequenceName="loftware.doc_cat_seq")
@Id
public Long getId(){
return id;
}
@Override
public void setId(Long id){
this.id = id;
}
@Override
@Transient
public int getAllPermissionsValue() {
return allPermissionsValue;
}
@Override
@Transient
public Boolean isParentListable() {
return false;
}
/**
* {@inheritDoc}
*
*/
@ManyToOne(fetch = FetchType.LAZY, targetEntity = DocumentCategoryModel.class)
@JoinColumn(name = "parent_id", nullable=true)
@ForeignKey(name = "FK_CAT_PARENT")
public DocumentCategory getParent() {
return parent;
}
public void setParent(DocumentCategory parent) {
this.parent = parent;
}
@OneToMany(fetch=FetchType.LAZY, targetEntity = DocumentCategoryModel.class, mappedBy = "parent")
@ForeignKey(name = "FK_CAT_PARENT")
public Set<DocumentCategory> getChildCategories() {
return childCategories;
}
public void setChildCategories(Set<DocumentCategory> childCategories) {
if (childCategories == null) {
this.childCategories.clear();
} else {
this.childCategories = childCategories;
}
}
public void setChildDocuments(Set<Document> childDocuments) {
if (childDocuments == null) {
this.childDocuments.clear();
}else{
this.childDocuments = childDocuments;
}
}
@Override
public void addChildCategory(DocumentCategory category) {
if (category != null) {
category.setParent(this);
getChildCategories().add(category);
}
}
@Override
public void addChildDocument(Document document) {
if (document != null) {
document.setParent(this);
getChildDocuments().add(document);
}
}
@Override
public void removeChildCategory(DocumentCategory category) {
if (category != null && childCategories.contains(category)) {
category.setParent(null);
childCategories.remove(category);
}
}
@Override
public void removeChildDocument(Document document) {
if (document != null && childDocuments.contains(document)) {
document.setParent(null);
childDocuments.remove(document);
}
}
@OneToMany( fetch = FetchType.LAZY, mappedBy="parent", targetEntity = DocumentModel.class)
@ForeignKey(name = "FK_DOCCAT_DOC")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
public Set<Document> getChildDocuments() {
return childDocuments;
}
@Override
@OneToMany(fetch = FetchType.LAZY, mappedBy = "identityObject", targetEntity=DocumentCategoryAclEntryModel.class, cascade=CascadeType.PERSIST)
@ForeignKey(name = "FK_DOCCAT_ACL_LIST")
@Sort(type=SortType.NATURAL)
public SortedSet<DocumentCategoryAclEntry> getAclEntries() {
return aclEntries ;
}
@Override
public void addAclEntry(AclEntryObject<DocumentCategory, DocumentCategory, Long> aclEntry) {
getAclEntries().add((DocumentCategoryAclEntry)aclEntry);
}
@Override
public void removeAclEntry(AclEntryObject<DocumentCategory, DocumentCategory, Long> aclEntry) {
SortedSet<DocumentCategoryAclEntry> acls = getAclEntries();
if(acls.contains(aclEntry)) acls.remove(aclEntry);
}
public void setAclEntries(SortedSet<DocumentCategoryAclEntry> entries){
this.aclEntries = entries;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
builder.append("name", getName());
return builder.toString();
}
@Override
@Transient
protected int getHashPrimary() {
return 7;
}
@Override
@Transient
protected int getHashSecondary() {
return 17;
}
@Override
@Transient
public Class<DocumentCategoryModel> getJavaType() {
return DocumentCategoryModel.class;
}
This is the test I'm running:
Code:
@Test
public void testGetDocument() throws Exception{
setUser(20l);
System.out.println("before");
DocumentCategory cat = documentCategoryDao.get(60l);
System.out.println("getting parent after this");
cat.getParent();
System.out.println("after");
}
This is the resulting System.out output is:
Code:
before
Hibernate:
select
documentca0_.id as id124_0_,
documentca0_.system as system124_0_,
documentca0_.modified as modified124_0_,
documentca0_.created as created124_0_,
documentca0_.created_issuer as created5_124_0_,
documentca0_.last_updated_issuer as last6_124_0_,
documentca0_.fully_qualified_name as fully7_124_0_,
documentca0_.entries_inheriting as entries8_124_0_,
documentca0_.name as name124_0_,
documentca0_.parent_id as parent10_124_0_
from
loftware.category documentca0_
where
documentca0_.id=?
Hibernate:
select
documentca0_.id as id124_0_,
documentca0_.system as system124_0_,
documentca0_.modified as modified124_0_,
documentca0_.created as created124_0_,
documentca0_.created_issuer as created5_124_0_,
documentca0_.last_updated_issuer as last6_124_0_,
documentca0_.fully_qualified_name as fully7_124_0_,
documentca0_.entries_inheriting as entries8_124_0_,
documentca0_.name as name124_0_,
documentca0_.parent_id as parent10_124_0_
from
loftware.category documentca0_
where
documentca0_.id=?
getting parent after this
after
As you can see its pulling the parent as part of the original fetch in a separate query.
documentCategoryDao is doing a simple entityManager.find(class,id);
so there is nothing special going on there.