[BioSQL-l] Load/Update Genbank sequence issue

Richard Holland dicknetherlands at gmail.com
Wed May 21 17:17:39 UTC 2008


I'm not sure why you'd want to do that? Either way, as far as I know,
Hibernate sessions are not designed for that. If you want to delete
the sequence then replace it with a completely new sequence object,
you have to create a new sequence object - you can't reuse the one you
just deleted. The session info is embedded into that object so even if
you clear the session, the object still refers to it.

If you just want to load a sequence, modify it, then save it back, you
should just load it then use saveOrUpdate to save it back again with
changes. If you want to replace objects in your database, don't load
them, just delete them, then use save to create new objects in their
place.

cheers,
Richard

2008/5/21 gang wu <gwu at molbio.mgh.harvard.edu>:
> I got another problem with almost the same code(see attached code and
> output). If I call the loadSeq() only once, everything works fine. The
> second call of the same method will fail. By the exception message, it looks
> like the session is not cleaned up yet though at the end of the method the
> session has been flush/clear/closed. Anything wrong?
>
> Or does anyone have a sequence data loader would like to share? I'm quite
> frustrated after keeping trying to do this seems simple thing without
> success. If I can make it work, I'd like to post it on the mailing list.
> Thanks in advance.
>
> Gang
>
> ====================================================
> public class SequenceLoaderNew {
>   public void loadSeq(String fileName, String namespace) throws
> FileNotFoundException, BioException, SQLException, InterruptedException {
>       SessionFactory sessionFactory = new
> Configuration().configure().buildSessionFactory();
>       Session session = sessionFactory.openSession();
>       RichObjectFactory.connectToBioSQL(session);
>       Namespace ns = (Namespace)
> RichObjectFactory.getObject(SimpleNamespace.class, new Object[]{namespace});
>       RichSequenceDB db = new BioSQLRichSequenceDB(session);
>       System.out.println("Start loading file " + fileName + " at " +
> System.currentTimeMillis());
>       BufferedReader br = new BufferedReader(new FileReader(fileName));
>       RichSequenceIterator rsi = RichSequence.IOTools.readGenbankDNA(br,
> ns);
>       try {
>           while (rsi.hasNext()) {
>               RichSequence sequence = rsi.nextRichSequence();
>               System.out.println("Loaded sequence" + sequence.getAccession()
> + ", identifier: " + sequence.getIdentifier());
>               Transaction tx = session.beginTransaction();
>               try {
>                   System.out.println("Delete sequence" +
> sequence.getAccession() + " if exists.");
>                   db.removeSequence(sequence.getAccession());
>                   tx.commit();
>                   session.flush();
>                   session.clear();
>               } catch (Exception ex) {
>                   if (tx != null) {
>                       tx.rollback();
>                   }
>               }
>               try {
>                   tx = session.beginTransaction();
>                   System.out.println("Save sequence " +
> sequence.getAccession());
>                   //session.save("BioEntry", sequence);
>                   //db.addSequence(sequence);
>                   db.addRichSequence(sequence);
>                   tx.commit();
>                   System.out.println("Finished savig sequence " +
> sequence.getAccession() + "\n");
>               } catch (HibernateException ex) {
>                   tx.rollback();
>                   ex.printStackTrace();
>               }
>           }
>       } finally {
>           session.flush();
>           session.clear();
>           session.close();
>       }
>   }
>
>   public static void main(String[] args) throws BioException,
> FileNotFoundException, InterruptedException, SQLException {
>       SequenceLoaderNew sl = new SequenceLoaderNew();
>       sl.loadSeq("/genomeseq/bacteria/NC_005813.gbk", "genbank");
>       sl.loadSeq("/genomeseq/bacteria/NC_005813.gbk", "genbank");
>   }
> }
> ====================================================
> Start loading file /genomeseq/bacteria/NC_005813.gbk at 1211349859602
> Loaded sequenceNC_005813, identifier: 45478502
> Delete sequenceNC_005813 if exists.
> Save sequence NC_005813
> Finished savig sequence NC_005813
>
> Start loading file /genomeseq/bacteria/NC_005813.gbk at 1211349863761
> Loaded sequenceNC_005813, identifier: 45478502
> Delete sequenceNC_005813 if exists.
> Save sequence NC_005813
> org.hibernate.NonUniqueObjectException: a different object with the same
> identifier value was already associated with the session: [Term#20]
>       at
> org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>       at
> org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
>       at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
>       at
> org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
>       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>       at
> org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
>       at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
>       at
> org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
>       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at
> org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
>       at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
>       at
> org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
>       at
> org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>       at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
>       at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
>       at primerdb.SequenceLoaderNew.loadSeq(SequenceLoaderNew.java:51)
>       at primerdb.SequenceLoaderNew.main(SequenceLoaderNew.java:69)
> Exception in thread "main" org.hibernate.NonUniqueObjectException: a
> different object with the same identifier value was already associated with
> the session: [Term#19]
>       at
> org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>       at
> org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
>       at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
>       at
> org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
>       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
>       at
> org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
>       at
> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>       at
> org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
>       at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
>       at
> org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
>       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at
> org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
>       at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
>       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
>       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
>       at
> org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
>       at
> org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
>       at
> org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
>       at
> org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
>       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
>       at primerdb.SequenceLoaderNew.loadSeq(SequenceLoaderNew.java:60)
>       at primerdb.SequenceLoaderNew.main(SequenceLoaderNew.java:69)
> ====================================================
>
> Richard Holland wrote:
>>
>> Forgot to copy my reply back to the lists!
>>
>>
>> ---------- Forwarded message ----------
>> From: Richard Holland <dicknetherlands at gmail.com>
>> Date: 2008/5/20
>> Subject: Re: [BioSQL-l] Load/Update Genbank sequence issue
>> To: gang wu <gwu at molbio.mgh.harvard.edu>
>>
>>
>> You need to substitute this line:
>>
>> RichSequenceIterator rsi = RichSequence.IOTools.readGenbankDNA(br, new
>> SimpleNamespace(namespace));
>>
>> with this line:
>>
>> Namespace ns =
>> (Namespace)RichObjectFactory.getObject(SimpleNamespace.class,new
>> Object[]{namespace});
>> RichSequenceIterator rsi = RichSequence.IOTools.readGenbankDNA(br, ns);
>>
>> cheers,
>> Richard
>>
>> PS. You're the second person who has quoted this section of code on
>> this list recently, and had the same problem with it. If you got it
>> from a website somewhere, could you send me the URL so I can get in
>> touch with the authors and get them to correct it? Thanks!
>>
>> 2008/5/20 gang wu <gwu at molbio.mgh.harvard.edu>:
>>
>>>
>>> Hi all,
>>>
>>> I'm trying to load Genbank sequence into BioSQL database with
>>> BioJavax/Hibernate. It's Ok to load the first sequence. Then I got the
>>> exception when loading new sequences or reloading the same sequence.
>>> Seems
>>> the Hibernate is trying to insert "genbank" namespace again. Can anybody
>>> tell me what I did wrong? Thanks in advance.
>>>
>>> Gang
>>>
>>> ================================================================
>>> public class SequenceLoaderNew {
>>>  public void loadSeq(String fileName, String namespace) throws
>>> FileNotFoundException, BioException {
>>>      System.out.println("Start loading file " + fileName + " at " +
>>> System.currentTimeMillis());
>>>      BufferedReader br = new BufferedReader(new FileReader(fileName));
>>>      Session session = new
>>> Configuration().configure().buildSessionFactory().openSession();
>>>      RichObjectFactory.connectToBioSQL(session);
>>>      RichSequenceDB db = new BioSQLRichSequenceDB(session);
>>>      RichSequenceIterator rsi = RichSequence.IOTools.readGenbankDNA(br,
>>> new
>>> SimpleNamespace(namespace));
>>>      while (rsi.hasNext()) {
>>>          Transaction tx = session.beginTransaction();
>>>          RichSequence sequence = rsi.nextRichSequence();
>>>          System.out.println("Loaded sequence" + sequence.getAccession() +
>>> ", identifier: " + sequence.getIdentifier());
>>>          try {
>>>              System.out.println("Delete sequence" +
>>> sequence.getAccession()
>>> + " if exists.");
>>>              db.removeSequence(sequence.getAccession());
>>>          } catch (Exception ex) {
>>>          }
>>>          try {
>>>              System.out.println("Save sequence " +
>>> sequence.getAccession());
>>>              session.save("BioEntry", sequence);
>>>              tx.commit();
>>>              session.flush();
>>>              session.clear();
>>>          } catch (HibernateException ex) {
>>>              tx.rollback();
>>>              ex.printStackTrace();
>>>          }
>>>      }
>>>  }
>>>
>>>  public static void main(String[] args) throws BioException,
>>> FileNotFoundException {
>>>      SequenceLoaderNew sl = new SequenceLoaderNew();
>>>      //sl.loadSeq("/genomeseq/bacteria/NC_005813.gbk", "genbank");
>>>      sl.loadSeq("/genomeseq/bacteria/NC_004349.gbk", "genbank");
>>>  }
>>> }
>>> ================================================================
>>> org.hibernate.exception.ConstraintViolationException: could not insert:
>>> [Namespace]
>>>      at
>>>
>>> org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
>>>      at
>>>
>>> org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
>>>      at
>>>
>>> org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
>>>      at
>>>
>>> org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
>>>      at
>>>
>>> org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
>>>      at
>>>
>>> org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
>>>      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>>>      at
>>> org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
>>>      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
>>>      at
>>> org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
>>>      at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
>>>      at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
>>>      at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
>>>      at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
>>>      at
>>>
>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
>>>      at
>>>
>>> org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
>>>      at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
>>>      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
>>>      at primerdb.SequenceLoaderNew.loadSeq(SequenceLoaderNew.java:38)
>>>      at primerdb.SequenceLoaderNew.main(SequenceLoaderNew.java:52)
>>> Caused by:
>>> com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:
>>> Duplicate entry 'genbank' for key 'name'
>>>      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1011)
>>>      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
>>>      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
>>>      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
>>>      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
>>>      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
>>>      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
>>>      at
>>>
>>> com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
>>>      at
>>>
>>> com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
>>>      at
>>>
>>> com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
>>>      at
>>>
>>> com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
>>>      at
>>>
>>> org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
>>>      at
>>>
>>> org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
>>>      ... 31 more
>>> ================================================================
>>>
>>> _______________________________________________
>>> BioSQL-l mailing list
>>> BioSQL-l at lists.open-bio.org
>>> http://lists.open-bio.org/mailman/listinfo/biosql-l
>>>
>>>
>
>



More information about the BioSQL-l mailing list