Menu

Inserting Binary objects...HELP??!!

2004-08-13
2013-04-11
  • Nobody/Anonymous

    Hi,

    I have been trying for some time now to insert  a binary object using iBatis, and  have been failing miserably. 

    Does anyone have an example they can post of this ?

    TIA

     
    • Larry Meadors

      Larry Meadors - 2004-08-13

      Hey! great timing, i have to do one of these today (using db2 and iBATIS 1.x) and will post the code here.

      I have another that uses M$SQL, too.

      What db and iBATIS version are you using?

       
    • Larry Meadors

      Larry Meadors - 2004-08-13

      Dang, this is too easy!

      I used byte[] for the blob in the bean that I put records into. In my dao class, I pass that to the sqlmap instance. the trickiest part of all of it is in the class that calls the dao where I create a byte[] with the data that I want to put into the record.

      I am on irc if you see this and have any questions in the next 2-3 hours (irc.darkmyst.org/6667 - #ibatis).

       
    • Nobody/Anonymous

      <<Dang, this is too easy!>>
      wow!!
      How far you are with the solution? May I know your blob size please?

       
      • Larry Meadors

        Larry Meadors - 2004-08-13

        These are pretty small - < 50k.

        We have done this with M$SQL with larger ones, but I do not have any hard numbers.

        Larry

         
    • Nobody/Anonymous

      I am using MySQL and iBatis 2.0. 

       
    • Nobody/Anonymous

      Hey Larry! 

      No code?? :-)  

       
      • Larry Meadors

        Larry Meadors - 2004-08-16

        Err, no, I did not put up any source - if your bean uses a byte[] (little "b" - not Byte[]), it just works. (With db2/400 and M$SQL, anyway.)

        The only source I could provide would be how to read a file into a byte[], but I am guessing you probably have that already.

        Larry

         
    • Nobody/Anonymous

      Actually that would be helpful for us newbies! 

      Also, I am really interested in seeing the xml parameter map definition for this, to see how it is set up.

       
      • Larry Meadors

        Larry Meadors - 2004-08-16
         
      • Larry Meadors

        Larry Meadors - 2004-08-16

        Ooops, the insert is this:

        INSERT INTO Document (
            FILENAME,
            DESCRIPTION,
            DOCUMENT,
            DOCUMENTTYPE,
            REMOVE,
            INSERTUSER,
            INSERTDATE
        ) VALUES (
            #filename#,
            #description#,
            #document#,
            #documentType#,
            #remove#,
            #insertUser#,
            #insertDate#
        )
        ===

        As you can see, it is no different from any other parameter type - byte[], String or Date would be the same.

        In the bean passed to this, I have tis:

        public class Document {
            private Integer documentId;
            private String filename;
            private String description;
            private byte[] document;
            private String documentType;
            private Date remove;
            private String updateUser;
            private Date updateDate;
            private String insertUser;
            private Date insertDate;
            //
            public byte[] getDocument() {
                return document;
            }
            public void setDocument(byte[] newValue) {
                document = newValue;
            }
            // put your other getters/setters here...
        }
        ===

        I left out the rest of the get/set methods to keep this from being a huge post, but they are nothing special, either.

        Does that help?

        Larry

         
    • Nobody/Anonymous

      You are the man, Larry!  Thanks!  I got the insert to work (I think...)!

      Could you also post a sample resultMap for querying this?  I keep geting an error :

      Caused by: com.ibatis.sqlmap.client.SqlMapException: No type handler could be found to map the property 'definition' to the column 'definition'.  One or both of the types, or the combination of types is not supported.

      Where definition is the field for the byte[] that is loaded.

       
      • Larry Meadors

        Larry Meadors - 2004-08-16

        Hmm, it is possible that the 2.x sql map handles this differently - my select looks like this:

        ===
        SELECT
            DOCUMENTID AS "documentId",
            FILENAME AS "filename",
            DESCRIPTION AS "description",
            DOCUMENT AS "document",
            DOCUMENTTYPE AS "documentType",
            REMOVE AS "remove",
            UPDATEUSER AS "updateUser",
            UPDATEDATE AS "updateDate",
            INSERTUSER AS "insertUser",
            INSERTDATE AS "insertDate"
        FROM Document
        WHERE documentId = #value#
        ===

        Larry

         
    • Nobody/Anonymous

      Thanks Larry!  You ARE the man!!!  I got the insert to work (I think...)

      Could you also post the resultMap for querying this object?

      When trying to retrieve this I keep getting an error :

      Caused by: com.ibatis.sqlmap.client.SqlMapException: No type handler could be found to map the property 'definition' to the column 'definition'.  One or both of the types, or the combination of types is not supported.

      Thanks!

       
    • Nobody/Anonymous

      GOT IT!!!!

      Thanks a ton Larry!  You have bailed me out!

       
      • Larry Meadors

        Larry Meadors - 2004-08-16

        Who is your daddy?!? ;-)

        Glad it's working.

         
    • Nobody/Anonymous

      Anyone did any wonderful work like this with Oracle!! Any sample like this!!!

       
    • Jason Erickson

      Jason Erickson - 2004-11-17

      This is great, and I got it working, but I still have a question.  What if I am concerned about very large objects.  If I am storing objects of several hundred MB, for example, getting the whole byte[] would be inappropriate.  I would want to use the stream from the result set.

      Is there any way to do this with iBATIS? If it is impossible with SQL Map, could you just do it separately but still within the DAO framework?

       
    • Nobody/Anonymous

      I am not aware of any drivers in java or C that stream a single byte field.

      .V

       
    • Larry Meadors

      Larry Meadors - 2004-11-19

      Hmmm, I would seriously consider other options if you have to put 100s of MB into a single LOB.

      Perhaps a path to a file on the file system would be a better approach in that case. You would have much finer control of the data access in that case (i.e., return any number of bytes at a time).

      You could even wrap that access to the file sytem in you DAO code to keep it abstracted from the application.

       

Log in to post a comment.