mbari-org / oni

Organism Naming Infrastructure
https://mbari-org.github.io/oni/docs
0 stars 0 forks source link

org.hibernate.HibernateException: More than one row with the given identifier was found: 5002526, for class: org.mbari.oni.jpa.entities.ConceptMetadataEntity #6

Open hohonuuli opened 1 month ago

hohonuuli commented 1 month ago

This exception is being throw even though there is only one matching identifier in the database. In think this is related to the @OneToOne join between concept and concept metadata. I'm referring to this link, https://stackoverflow.com/questions/24210478/hibernate-more-than-one-row-with-the-given-identifier-was-found-error for a solution.

hohonuuli commented 1 month ago

I changed the query for Concept.findByName:

--- a/oni/src/main/java/org/mbari/oni/jpa/entities/ConceptEntity.java
+++ b/oni/src/main/java/org/mbari/oni/jpa/entities/ConceptEntity.java
@@ -31,11 +31,12 @@ import org.mbari.oni.jpa.IPersistentObject;
 @NamedQueries( {
     @NamedQuery(name = "Concept.eagerFindById", query = "SELECT c FROM Concept c JOIN FETCH c.conceptMetadata m WHERE c.id = :id"),
     @NamedQuery(name = "Concept.findAll", query = "SELECT c FROM Concept c"),
-    @NamedQuery(name = "Concept.findAllByNameGlob", query = "SELECT DISTINCT c FROM Concept c, IN (c.conceptNames) AS n WHERE LOWER(n.name) LIKE :name"),
-    @NamedQuery(name = "Concept.findAllByNameGlobNew", query = "SELECT c FROM Concept c JOIN FETCH c.conceptNames n WHERE LOWER(n.name) LIKE :name"),
+    @NamedQuery(name = "Concept.findAllByNameGlob", query = "SELECT DISTINCT c FROM Concept c LEFT JOIN c.conceptNames n WHERE LOWER(n.name) LIKE :name"),
+    @NamedQuery(name = "Concept.findAllByNameGlobNew", query = "SELECT c FROM Concept c LEFT JOIN c.conceptNames n WHERE LOWER(n.name) LIKE :name"),
     @NamedQuery(name = "Concept.findByAphiaId", query = "SELECT c FROM Concept c WHERE c.aphiaId = :aphiaId") ,
     @NamedQuery(name = "Concept.findById", query = "SELECT v FROM Concept v WHERE v.id = :id") ,
-    @NamedQuery(name = "Concept.findByName", query = "SELECT c FROM Concept c, IN (c.conceptNames) AS n WHERE n.name = :name"),
+    @NamedQuery(name = "Concept.findByName", query = "SELECT c FROM Concept c LEFT JOIN c.conceptNames n WHERE n.name = :name"),
+//    @NamedQuery(name = "Concept.findByName", query = "SELECT c FROM Concept c, IN (c.conceptNames) AS n WHERE n.name = :name"),
     @NamedQuery(name = "Concept.findByRankLevel", query = "SELECT c FROM Concept c WHERE c.rankLevel = :rankLevel") ,
     @NamedQuery(name = "Concept.findByRankName", query = "SELECT c FROM Concept c WHERE c.rankName = :rankName"),
     @NamedQuery(name = "Concept.findRoot", query = "SELECT c FROM Concept c WHERE c.parentConcept IS NULL")
hohonuuli commented 1 month ago

OK, found out that the DARC database does have duplicate records:

SELECT
    cd.conceptid_fk,
    count(*) as n
FROM
    conceptdelegate cd
GROUP BY cd.conceptid_fk
HAVING count(*) > 1
ORDER BY conceptid_fk ASC

returns 191 rows with more than one concept delegate per concept

hohonuuli commented 1 month ago
SELECT
    *
FROM
    ConceptDelegate
WHERE
    conceptdelegate.conceptid_fk IN (SELECT cd.conceptid_fk
           FROM conceptdelegate cd
           GROUP BY cd.conceptid_fk
           HAVING count(*) > 1)
ORDER BY conceptid_fk ASC
hohonuuli commented 1 month ago

Looks like the old KB app has a bug where it can create an extra conceptdelegate.

hohonuuli commented 1 month ago

It looks like the duplicates are created during adding history:

SELECT
    *
FROM history
WHERE conceptdelegateid_fk IN (SELECT id
                               FROM ConceptDelegate
                               WHERE conceptdelegate.conceptid_fk IN (SELECT cd.conceptid_fk
                                                                      FROM conceptdelegate cd
                                                                      GROUP BY cd.conceptid_fk
                                                                      HAVING count(*) > 1)
                               ORDER BY conceptid_fk ASC)
ORDER BY conceptdelegateid_fk DESC
hohonuuli commented 1 month ago

I added a script, oni_issue6.sh, that removes the duplicate problems. I can't fully fix the issue until the new kb app is done. So here's the steps to resolve: