public void setId(Long id) { if (this.id != null) { throw new IllegalStateException(this + " id cannot be changed"); } this.getChanged().record("id", this.id, id); this.id = id; if (UoW.isOpen()) { UoW.getIdentityMap().store(this); } }
public P get() { if (this.instance == null && this.id != null) { if (!UoW.isOpen()) { throw new DisconnectedException(); } if (!EagerLoading.isEnabled()) { // will make a query for just this id, only if needed this.instance = UoW.load(this.parentClass, this.id); } else { // see if the parent is loaded, but don't make a query while doing so (like UoW.load does) this.instance = (P) UoW.getIdentityMap().findOrNull(this.parentClass, this.id); if (this.instance == null) { // get the parent ids of all currently-loaded child classes Collection<Long> parentIds = new LinkedHashSet<Long>(); for (C child : UoW.getIdentityMap().getInstancesOf(this.childClass)) { Long parentId = this.childColumn.getJdbcValue(child); if (parentId != null) { parentIds.add(parentId); } } // subtract any ids that are already loaded for (P parent : UoW.getIdentityMap().getInstancesOf(this.parentClass)) { parentIds.remove(parent.getId()); } // if even our own id was not in the list, that is bad, we're from another UoW if (parentIds.size() == 0) { throw new IllegalStateException("Instance has been disconnected from the UoW"); } Select<P> q = Select.from(this.parentAlias); q.where(this.parentAlias.getIdColumn().in(parentIds)); q.limit(UoW.getIdentityMap().getCurrentSizeLimit()); q.list(); // will populate the UoW IdentityMap with all fetched parents this.instance = (P) UoW.getIdentityMap().findOrNull(this.parentClass, this.id); } } } return this.instance; }
public Long id() { if (UoW.isOpen() && get().getId() == null) { UoW.flush(); } return get().getId(); }