Hi , i am using replicate ids to make queries much faster inline for objectdb.
But this makes duplicate ids.Is there any better way to use them on objectdb without duplicate and keep performance fast.
I dont want to use joins they too slow ???
package com.manage.jpa.pollen;
import com.manage.jpa.GenericEntity;
import com.objectdb.Index;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.NaturalId;
import org.springframework.cache.annotation.Cacheable;
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Getter
@Setter
@Cacheable
public class PollenDefinitionEntity extends GenericEntity {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pollenDivisionEntityId", referencedColumnName = "idValue", nullable = false)
private PollenDivisionEntity pollenDivisionEntity;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pollenFamilyEntityId", referencedColumnName = "idValue", nullable = false)
private PollenFamilyEntity pollenFamilyEntity;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pollenGenusEntityId", referencedColumnName = "idValue", nullable = true)
private PollenGenusEntity pollenGenusEntity;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pollenSpeciesEntityId", referencedColumnName = "idValue", nullable = true)
private PollenSpeciesEntity pollenSpeciesEntity;
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] picture;
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] icon;
// denormalized ids for direct access
@Column(name = "pollenDivisionEntityId", insertable = false, updatable = false)
@Index
private Long pollenDivisionEntityId;
@Column(name = "pollenFamilyEntityId", insertable = false, updatable = false)
@Index
private Long pollenFamilyEntityId;
@Column(name = "pollenGenusEntityId", insertable = false, updatable = false)
@Index
private Long pollenGenusEntityId;
@Column(name = "pollenSpeciesEntityId", insertable = false, updatable = false)
@Index
private Long pollenSpeciesEntityId;
@Column(nullable = false)
private int referenceCounter;
private void syncIds() {
pollenDivisionEntityId = pollenDivisionEntity.getIdValue();
pollenFamilyEntityId = pollenFamilyEntity.getIdValue();
pollenGenusEntityId = (pollenGenusEntity != null) ? pollenGenusEntity.getIdValue() : null;
pollenSpeciesEntityId = (pollenSpeciesEntity != null) ? pollenSpeciesEntity.getIdValue() : null;
}
@Override
protected void onPrePersist() {
syncIds();
}
@Override
protected void onPreUpdate() {
syncIds();
}
@Override
public String toString() {
return "PollenDefinitionEntity{" +
"pollenDivisionEntity=" + (pollenDivisionEntity == null ? null : pollenDivisionEntity.getIdValue()) +
", pollenFamilyEntity=" + (pollenFamilyEntity == null ? null : pollenFamilyEntity.getIdValue()) +
", pollenGenusEntity=" + (pollenGenusEntity == null ? null : pollenGenusEntity.getIdValue()) +
", pollenSpeciesEntity=" + (pollenSpeciesEntity == null ? null : pollenSpeciesEntity.getIdValue()) +
'}';
}
} package com.manage.jpa.pollen;
import com.manage.jpa.repo.BaseRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface PollenDefinitionRepository extends BaseRepository<PollenDefinitionEntity,Long,Long> { @Query(" SELECT d FROM PollenDefinitionEntity d WHERE " +
"(d.pollenDivisionEntityId = :divisionId) AND " +
"(d.pollenFamilyEntityId = :familyId) AND " +
"( (:genusId IS NULL AND d.pollenGenusEntityId IS NULL) OR d.pollenGenusEntityId = :genusId ) AND" +
"( (:speciesId IS NULL AND d.pollenSpeciesEntityId IS NULL) OR d.pollenSpeciesEntityId = :speciesId )")
Optional<PollenDefinitionEntity> findByIds(
@Param("divisionId") Long divisionId,
@Param("familyId") Long familyId,
@Param("genusId") Long genusId,
@Param("speciesId") Long speciesId
); }
