-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: mapping two dimensional byte array to single database column
PostPosted: Sat Mar 07, 2009 6:49 am 
Newbie

Joined: Thu Oct 30, 2008 3:26 am
Posts: 10
Hi,
I need some help in mapping mapping a two dimensional array to a single database column.
I need to store some encrypted messages array. My business do not allow to create sub table for storing message or adding some more columns.
I need to store them in blob field.

Code:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into testobj (messages, id) values (?, ?)
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
   at test.TestObj$$EnhancerByCGLIB$$a5febea4.getMessages(<generated>)
   at test.TestDAO.main(TestDAO.java:54)



POJO class
---------------
Code:
public class TestObj {

   private int id;
   private byte[][] messages;
}


DAO
--------
Code:
public class TestDAO extends HibernateDaoSupport {

   protected void initDao() {
      // do nothing
   }
   public void save(TestObj obj){
      getHibernateTemplate().save(obj);
   }
   public TestObj load(int id){
      
      return (TestObj)getHibernateTemplate().load(TestObj.class, id);
   }
   
   public static TestDAO getFromApplicationContext(
         ApplicationContext ctx) {
      return (TestDAO) ctx.getBean("TestDAO");
   }
   public static void main(String[] args) {
      
      String message1 = "Message1";
      String message2 = "Message2";
      
      byte[][] array = new byte[2][];
      array[0] = message1.getBytes();
      array[1] = message2.getBytes();
      
      TestObj obj = new TestObj();
      obj.setId(1);
      obj.setMessages(array);
      
      TestDAO dao = TestDAO.getFromApplicationContext(AppContext.getContext());
      dao.save(obj);
      
      obj = dao.load(1);
      
      byte[][] msgs = obj.getMessages();
      
      System.out.println(new String(msgs[0]));
      System.out.println(new String(msgs[1]));
   }
}

mapping file
---------------
Code:
<hibernate-mapping>
   <class name="test.TestObj" table="testobj" >
      <id name="id" >
         <column name="id" precision="22" scale="0" />
         <generator class="assigned" />
      </id>
      <property name="messages" lazy="false">
         <column name="messages" />
      </property>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 29, 2009 3:55 am 
Newbie

Joined: Tue Dec 30, 2008 3:29 pm
Posts: 4
Location: Netherlands
Hi,

I'm not sure what exactly is the problem in your code, it just works fine for me with PostgreSQL, although without Spring support. However you can always convert any object - even a two-dimensional array - into a one-dimensional byte array easily with the help of the ByteArrayOutputStream class. A one-dimensional byte array will be mapped to a blob field (in PostgreSQL its bytea) for sure.
You have to add the getter/setter methods to your POJO wich do the conversion, and make the mappings accordingly. These getter/setter methods might be package-private so they are not exposed to the user still Hibernate can use them.

Code:
public class TestObj {
    private int id;
    private byte[][] messages;

    public int getId() {
        return id;
    }

    public byte[][] getMessages() {
        return messages;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setMessages(byte[][] messages) {
        this.messages = messages;
    }

    byte[] getMessagesArray() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(messages);

        return baos.toByteArray();
    }

    void setMessagesArray(byte[] messagesArray) throws ClassNotFoundException, IOException {
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(messagesArray));
        messages = (byte[][]) ois.readObject();
    }
}


And the mapping xml is:

Code:
<hibernate-mapping>
  <class name="TestObj" table="testobj">
    <id name="id" >
      ...
    </id>
    <property name="messagesArray" lazy="false">
      <column name="messagesArray" />
    </property>
  </class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.