[Biojava-l] Exception org.hibernate.NonUniqueObjectException

Augusto Fernandes Vellozo augustovmail-java at yahoo.com.br
Wed Aug 20 13:36:27 UTC 2008


Hi,
I am trying to load a lot of features from one file to MYSQL and i am having
problems to do this with BIOJAVA/hibernate.
If I don't do the flush/clear in the session, i have one exception like
OutOfMemory.

But, after I do the flush/clear, the second query throws the exception:
org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session: [Term#23755]

I've already tried to clean the RichObjectFactory, but it doesn't work.

Please, some one knows what could be happening? Some suggestion?
The code is below.

Thanks,
-- 
Augusto F. Vellozo

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.TreeSet;

import org.biojava.bio.BioException;
import org.biojavax.RichObjectFactory;
import org.biojavax.SimpleRichAnnotation;
import org.biojavax.bio.seq.RichFeature;
import org.biojavax.bio.seq.SimplePosition;
import org.biojavax.bio.seq.SimpleRichFeature;
import org.biojavax.bio.seq.SimpleRichLocation;
import org.biojavax.bio.seq.RichLocation.Strand;
import org.biojavax.bio.taxa.NCBITaxon;
import org.biojavax.ontology.SimpleComparableOntology;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class LoadORFVRTest
{
    public static void main(String[] args) {
        SessionFactory sessionFactory = new
Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        RichObjectFactory.connectToBioSQL(session);

RichObjectFactory.setDefaultNamespaceName(Messages.getString("nameSpaceDefault"));
        Transaction tx = session.beginTransaction();
        try {
            //file orfs
            File fileOrfs;
            fileOrfs = new File(args[0]);

            String orfName, geneName = "";
            BufferedReader br = new BufferedReader(new
FileReader(fileOrfs));
            String line, line2, line3, lineAmino;
            int countOrfs = 0;
            int beginPos = -1, endPos = -1, nextPos = -1;
            int strand = 0;
            int stepORF =
Integer.parseInt(Messages.getString("LoadORFVR.printORF"));
            while ((line = br.readLine()) != null) {
                if (line.length() > 0) {
                    if (line.startsWith(">")) { //ORF heading
                        //new ORF
                        //save last ORF
                        if (strand != 0) {
                            saveORF(session, strand, beginPos, endPos,
nextPos - 1, geneName, Integer.parseInt(args[1]));
                            countOrfs++;
                        }
                        if (countOrfs % stepORF == 0) {
                            System.out.println(countOrfs);
                            session.flush();
                            tx.commit();
                            session.clear();
                            session.close();
                            RichObjectFactory.clearLRUCache();
                            session = sessionFactory.openSession();
                            RichObjectFactory.connectToBioSQL(session);

RichObjectFactory.setDefaultNamespaceName(Messages.getString("nameSpaceDefault"));
                            tx = session.beginTransaction();
                        }
                        orfName = line.substring(1);
                        geneName = orfName.substring(0,
orfName.indexOf("_"));
                        line = br.readLine();

                        if (line.startsWith("Reading frame: ")) {
                            strand = Integer.parseInt(line.substring(15));
                            if (strand == 0) {
                                System.out.println("Format error, strand =
0");
                            }
                            else {
                                nextPos = 1;
                                beginPos = -1;
                                endPos = -1;
                            }
                        }
                        else {
                            System.out.println("Format error in line
'Reading frame':" + line);
                            strand = 0;
                        }
                        br.readLine(); // empty line
                    }
                    else if (strand != 0) { //ORF sequence
                        line2 = br.readLine();
                        line3 = br.readLine();
                        br.readLine(); // empty line
                        if (strand < 0) {
                            lineAmino = line3;
                        }
                        else {
                            lineAmino = line;
                        }
                        lineAmino = lineAmino.substring(3,
lineAmino.length() - 1);
                        if (lineAmino.trim().length() != 0) {
                            if (beginPos < 0) {
                                beginPos = nextPos +
firstPosNotSpace(lineAmino) - 1;
                            }
                            endPos = nextPos + lastPosNotSpace(lineAmino) +
1;
                        }
                        nextPos += lineAmino.length();
                    }
                }
            }
            if (strand != 0) {
                saveORF(session, strand, beginPos, endPos, nextPos - 1,
geneName, Integer.parseInt(args[1]));
            }
            session.flush();
            tx.commit();
            session.clear();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            session.close();
        }

    }

    public static void saveORF(Session session, int strand, int beginPos,
int endPos, int lastPos, String geneName,
            int ncbiTaxonId) throws BioException {
        SimplePosition beginPosition, endPosition;
        if (strand < 0 && beginPos < 4) {
            beginPosition = new SimplePosition(true, false, beginPos);
        }
        else {
            beginPosition = new SimplePosition(beginPos);
        }
        if (strand > 0 && (endPos == lastPos)) {
            endPosition = new SimplePosition(false, true, endPos);
        }
        else {
            endPosition = new SimplePosition(endPos);
        }
        //                            save;
        NCBITaxon taxon = (NCBITaxon) session.createQuery("from Taxon where
ncbi_taxon_id=:ncbiTaxonNumber").setInteger(
            "ncbiTaxonNumber", ncbiTaxonId).uniqueResult();

        SimpleComparableOntology ontFeatures = (SimpleComparableOntology)
RichObjectFactory.getObject(
            SimpleComparableOntology.class, new Object[]
{Messages.getString("ontologyFeatures")});
        SimpleComparableOntology ontGeneral = ((SimpleComparableOntology)
RichObjectFactory.getObject(
            SimpleComparableOntology.class, new Object[]
{Messages.getString("ontologyGeneral")}));
        SimpleRichFeature featureGene = (SimpleRichFeature)
session.createQuery(
            "select f from Feature as f join f.parent as b where "
                + "f.name=:geneName and f.typeTerm=:geneTerm and
b.taxon=:taxonId ").setString("geneName", geneName).setParameter(
            "taxonId", taxon).setParameter("geneTerm",
ontFeatures.getOrCreateTerm(Messages.getString("termGene"))).uniqueResult();
        RichFeature.Template ft = new RichFeature.Template();
        ft.location = featureGene.getLocation().translate(0);
        ft.sourceTerm =
ontGeneral.getOrCreateTerm(Messages.getString("termVR"));
        ft.typeTerm =
ontFeatures.getOrCreateTerm(Messages.getString("termMRNA"));
        ft.annotation = new SimpleRichAnnotation();
        ft.featureRelationshipSet = new TreeSet();
        ft.rankedCrossRefs = new TreeSet();
        SimpleRichFeature featureMRNA = (SimpleRichFeature)
featureGene.createFeature(ft);
        featureMRNA.setName(geneName);

        ft = new RichFeature.Template();
        if (strand < 0) {
            ft.location = new SimpleRichLocation(beginPosition, endPosition,
0, Strand.NEGATIVE_STRAND);
        }
        else {
            ft.location = new SimpleRichLocation(beginPosition, endPosition,
0, Strand.POSITIVE_STRAND);
        }
        ft.sourceTerm =
ontGeneral.getOrCreateTerm(Messages.getString("termVR"));
        ft.typeTerm =
ontFeatures.getOrCreateTerm(Messages.getString("termORF"));
        ft.annotation = new SimpleRichAnnotation();
        ft.featureRelationshipSet = new TreeSet();
        ft.rankedCrossRefs = new TreeSet();
        SimpleRichFeature featureORF = (SimpleRichFeature)
featureMRNA.createFeature(ft);
        featureORF.setName(geneName);
    }

    public static int firstPosNotSpace(String str) {
        int i = 0;
        while (i < str.length() && str.charAt(i) == ' ') {
            i++;
        }
        return i;
    }

    public static int lastPosNotSpace(String str) {
        int i = str.length() - 1;
        while (i >= 0 && str.charAt(i) == ' ') {
            i--;
        }
        return i;
    }
}



More information about the Biojava-l mailing list