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>