Announcement Announcement Module
Collapse
No announcement yet.
problem with mapping byte array in object to Blob type in DB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with mapping byte array in object to Blob type in DB

    I have one java bean which represent the user uploaded file object. The file I got is in byte array value and I want to store this value in database. In the file bean class I define the property:
    Code:
    ...
    @Transient
    	private byte[] file;
    ...
    //byte stream
    public void setFile(byte[] file) {
            this.file = file;
        }
    
     public byte[] getFile() {
            return file;
        }
    ...
    I want to use a Blob type in data base to store this byte array value as one column. How to map byte array type property to Blob type in database???

    (I use Spring+hibernate3)

  • #2
    I'm pretty sure I've seen this question come up several times before. Have you tried searching through the previous answers.
    Last edited by karldmoore; Aug 27th, 2007, 04:30 PM.

    Comment


    • #3
      I found one thread about this mapping issue in the forum:
      http://forum.springframework.org/sho...highlight=Clob

      It is all about oracle DB. In the thread, jmays5150 give a illustration of how to map. And I follow the steps. The problem now is I use MySQL database not oracle. so , after I create my mapping file

      mappingfile.hbm.xml :
      Code:
      <hibernate-mapping>
      <class
      		name="org.example.Customer"
      		table="CUSTOMERS">
      <property
      			name="name"
      			type="java.lang.String"
      			column="NAME"
      			length="1000" />
      <property
      			name="description"
      			type="org.springframework.orm.hibernate.support.ClobStringType"
      			column="DESCRIPTION"
      			not-null="true" />
      </class>
      </hibernate-mapping>
      I need to configure something in applicationContext.xml:
      Code:
         <!-- Database Connections -->
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
            <property name="url"><value>myConnectionString</value></property>
            <property name="username"><value>myUsername</value></property>
            <property name="password"><value>myPassword</value></property>
            <property name="defaultAutoCommit"><value>false</value></property>
            <property name="maxWait"><value>3000</value></property>
            <property name="maxIdle"><value>100</value></property>
            <property name="maxActive"><value>10</value></property>
         </bean>
            
            <!-- NativeJdbcExtractor -->
         <bean id="nativeJdbcExtractor"
            class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"
            lazy-init="true"/>
      
         <!-- LobHandler for Oracle JDBC drivers -->
         <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
            <property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
         </bean>
         
          <!-- Hibernate SessionFactory -->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
              <property name="dataSource"><ref bean="dataSource"/></property>
              <property name="lobHandler"><ref bean="oracleLobHandler"/></property>
              <property name="mappingResources">
                  <list>
                  <value>mappingfile.hbm.xml</value>
                  </list>
              </property>
      Since I use MySQL database instead of oracle, the bold place need to change, how to change that in order to be suitable for MySQL DB in this caes???


      Another question is if I use annotation in my object bean, like:
      Code:
      public class xyz{
      .....
      @Basic
      @Column(name="NAME")
      String name;
      ....
      public String getName()....
      public void setName(String name)...
      }
      If I use @Transaction annotation for each property in my object bean class, do I really need to write my mappingfile.hbm.xml like the top shown , again describe each column one by one?(you can check my mappingfile.hbm.xml above), because I am doubt that file doing duplicated work as annotation. If so, how to write the mappingfile.hbm.xml mapping file if I use @ Transaction annoation in object bean class?
      Last edited by blust1984; Jul 19th, 2007, 06:49 AM.

      Comment


      • #4
        any suggestion?

        Comment


        • #5
          sandy

          when every thing is correct b/w hbm and class am getting this error plz give me sol.


          ----
          org.hibernate.MappingException: Could not determine type for: org.springframework.orm.hibernate.support.ClobStri ngType, for columns: [org.hibernate.mapping.Column(IMAGE)]
          org.hibernate.mapping.SimpleValue.getType(SimpleVa lue.java:266)
          org.hibernate.mapping.SimpleValue.isValid(SimpleVa lue.java:253)
          org.hibernate.mapping.Property.isValid(Property.ja va:185)
          org.hibernate.mapping.PersistentClass.validate(Per sistentClass.java:440)
          org.hibernate.mapping.RootClass.validate(RootClass .java:192)
          org.hibernate.cfg.Configuration.validate(Configura tion.java:1102)
          org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1287)
          org.springframework.orm.hibernate3.LocalSessionFac toryBean.newSessionFactory(LocalSessionFactoryBean .java:800)
          org.springframework.orm.hibernate3.LocalSessionFac toryBean.afterPropertiesSet(LocalSessionFactoryBea n.java:726)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1059)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:363)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
          org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:176)
          org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:105)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1013)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:824)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:345)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
          org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:176)
          org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:105)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1013)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:824)
          org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:345)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:226)
          org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)
          org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:275)
          org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:320)
          org.springframework.web.context.support.AbstractRe freshableWebApplicationContext.refresh(AbstractRef reshableWebApplicationContext.java:134)
          org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:3 05)
          org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:250 )
          org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:219)
          org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:112)
          javax.servlet.GenericServlet.init(GenericServlet.j ava:212)
          org.apache.jasper.runtime.PageContextImpl.doForwar d(PageContextImpl.java:691)
          org.apache.jasper.runtime.PageContextImpl.forward( PageContextImpl.java:661)
          org.apache.jsp.jsp.welcome_jsp._jspService(welcome _jsp.java:51)
          org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:98)
          javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
          org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:328)
          org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:315)
          org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:265)
          javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

          Comment

          Working...
          X