I have an entity that can have 0 or many children of the same entity. Also, the entity can have 0 or 1 parent of the same entity. This is a legacy table structure and I am unable to modify it.
Tables StructureCode:
Account (
AccountId PK,
Name char
etc...
)
AccountRef (
ChildId PK, FK references Account(AccountId)
ParentId PK, FK references Account(AccountId)
)
Everything is working correctly except any time I load an Account, a join is always done on the join table (I'm assuming to see if there is an AccountRef record), even though it is annotated Lazy. Also, it looks like hibernate is treating a ManyToOne on a JoinTable as a SecondaryTable?
Excerpt from log
Quote:
DEBUG org.hibernate.cfg.AnnotationBinder Processing annotations of Account.masterAccount
DEBUG org.hibernate.cfg.Ejb3Column Binding column: Ejb3JoinColumn{logicalColumnName='parentId', referencedColumn='accountId', mappedBy=''}
DEBUG org.hibernate.cfg.Ejb3Column Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Account), mappingColumn=masterAccount, insertable=true, updatable=true, unique=false}
INFO org.hibernate.cfg.annotations.EntityBinder Adding secondary table to entity Account -> AccountRef
DEBUG org.hibernate.cfg.annotations.PropertyBinder Building property masterAccount
DEBUG org.hibernate.cfg.annotations.PropertyBinder Cascading masterAccount with none
Is the only way around this to use instrumentation for LazyToOne, or is there a better way to map this?
Code:
@Entity
Account {
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="masterAccount")
private Set<Account> subAccount;
@ManyToOne(fetch=FetchType.LAZY)
@JoinTable(name="AccountRef",
joinColumns = @JoinColumn(name="childId", referencedColumnName="accountId"),
inverseJoinColumns = @JoinColumn(name="parentId", referencedColumnName="accountId")
)
private Account masterAccount;