2 Replies Latest reply on Aug 21, 2006 2:17 PM by ccharpentier

    get Object field in MS SQL

    ccharpentier

      I've got jboss 4.0.4GA with MS SQLSERVER

      I want to create an Image type field in MS SQL within an ejb3.

      when i look in the mapping file standardjaws.xml

      <mapping>
       <java-type>java.lang.Object</java-type>
       <jdbc-type>JAVA_OBJECT</jdbc-type>
       <sql-type>IMAGE</sql-type>
       </mapping>
      


      here this is my entity

      import java.sql.Date;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      import org.hibernate.annotations.Type;
      
      @Entity
      @Table(name="FichierOma")
      public class FichierOma {
      
       Integer id;
       String numCommande;
       String numJob;
       String idClient;
       Date insertDate;
       Object fichierOma;
       /**
       * @return Returns the fichierOma.
       */
      
       public Object getFichierOma() {
       return fichierOma;
       }
       /**
       * @param fichierOma The fichierOma to set.
       */
       public void setFichierOma(Object fichierOma) {
       this.fichierOma = fichierOma;
       }
      
       /**
       * @return Returns the id.
       */
       @Id
       @GeneratedValue
       public Integer getId() {
       return id;
       }
       /**
       * @param id The id to set.
       */
       public void setId(Integer id) {
       this.id = id;
       }
       /**
       * @return Returns the idClient.
       */
       public String getIdClient() {
       return idClient;
       }
       /**
       * @param idClient The idClient to set.
       */
       public void setIdClient(String idClient) {
       this.idClient = idClient;
       }
       /**
       * @return Returns the insertDate.
       */
       public Date getInsertDate() {
       return insertDate;
       }
       /**
       * @param insertDate The insertDate to set.
       */
       public void setInsertDate(Date insertDate) {
       this.insertDate = insertDate;
       }
       /**
       * @return Returns the numCommande.
       */
       public String getNumCommande() {
       return numCommande;
       }
       /**
       * @param numCommande The numCommande to set.
       */
       public void setNumCommande(String numCommande) {
       this.numCommande = numCommande;
       }
       /**
       * @return Returns the numJob.
       */
       public String getNumJob() {
       return numJob;
       }
       /**
       * @param numJob The numJob to set.
       */
       public void setNumJob(String numJob) {
       this.numJob = numJob;
       }
      
      }
      


      when I deploy this ejb3
      i get this error

      ObjectName: persistence.units:jar=formOma.par.jar,unitName=formoma
       State: FAILED
       Reason: org.hibernate.MappingException: property mapping has wrong number of c
      olumns: com.edieyes.oma.entities.FichierOma.fichierOma type: object
      



      If i replace Object by String in my entity everything is OK.

      if somebody have a clue?

      thanks

        • 1. Re: get Object field in MS SQL
          ccharpentier

          Hi,

          I managed to crete my image field in MS SQL to get file larger than 8Ko

          I create a Blob in my entity

          package com.edieyes.oma.entities;
          
          import java.io.InputStream;
          import java.sql.Blob;
          import java.sql.Date;
          
          import javax.persistence.Basic;
          import javax.persistence.Entity;
          import javax.persistence.FetchType;
          import javax.persistence.GeneratedValue;
          import javax.persistence.Id;
          import javax.persistence.Lob;
          import javax.persistence.Table;
          
          import org.hibernate.annotations.Type;
          
          @Entity
          @Table(name="FichierOma")
          public class FichierOma {
          
           Integer id;
           String numCommande;
           String numJob;
           String idClient;
           Date insertDate;
           Blob fichierOma;
           /**
           * @return Returns the fichierOma.
           */
           @Lob @Basic(fetch = FetchType.EAGER)
           public Blob getFichierOma() {
           return fichierOma;
           }
           /**
           * @param fichierOma The fichierOma to set.
           */
           public void setFichierOma(Blob fichierOma) {
           this.fichierOma = fichierOma;
           }
          
           /**
           * @return Returns the id.
           */
           @Id
           @GeneratedValue
           public Integer getId() {
           return id;
           }
           /**
           * @param id The id to set.
           */
           public void setId(Integer id) {
           this.id = id;
           }
           /**
           * @return Returns the idClient.
           */
           public String getIdClient() {
           return idClient;
           }
           /**
           * @param idClient The idClient to set.
           */
           public void setIdClient(String idClient) {
           this.idClient = idClient;
           }
           /**
           * @return Returns the insertDate.
           */
           public Date getInsertDate() {
           return insertDate;
           }
           /**
           * @param insertDate The insertDate to set.
           */
           public void setInsertDate(Date insertDate) {
           this.insertDate = insertDate;
           }
           /**
           * @return Returns the numCommande.
           */
           public String getNumCommande() {
           return numCommande;
           }
           /**
           * @param numCommande The numCommande to set.
           */
           public void setNumCommande(String numCommande) {
           this.numCommande = numCommande;
           }
           /**
           * @return Returns the numJob.
           */
           public String getNumJob() {
           return numJob;
           }
           /**
           * @param numJob The numJob to set.
           */
           public void setNumJob(String numJob) {
           this.numJob = numJob;
           }
          
          }
          


          and I get in my SQL SERVER

          id : int
          idClient : varchar(255)
          fichierOma : image
          insertDate : datetime
          numCommande : varchar(255)
          numJob : varchar(255)

          but now I ve got another problem

          when I want to insert an entity I get this error message

          javax.ejb.EJBException: java.lang.IllegalStateException: Blobs may not be access
          ed after serialization
           at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.ja
          va:69)
           at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
           at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java
          :197)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInt
          erceptor.java:76)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(Stateles
          sInstanceInterceptor.java:62)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.aspects.security.AuthenticationInterceptor.invoke(Authentic
          ationInterceptor.java:78)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterce
          ptor.java:47)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(Asynchrono
          usInterceptor.java:106)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessCo
          ntainer.java:227)
           at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.
          java:59)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteP
          roxy.java:102)
           at $Proxy169.addfile(Unknown Source)
           at com.edieyes.oma.servlet.Test2Servlet.createUsers(Test2Servlet.java:96
          )
           at com.edieyes.oma.servlet.Test2Servlet.doPost(Test2Servlet.java:68)
           at com.edieyes.oma.servlet.Test2Servlet.doGet(Test2Servlet.java:63)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
          icationFilterChain.java:252)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
          ilterChain.java:173)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
          lter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
          icationFilterChain.java:202)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
          ilterChain.java:173)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
          alve.java:213)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
          alve.java:178)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
          yAssociationValve.java:175)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
          e.java:74)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
          ava:126)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
          ava:105)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
          ve.java:107)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
          a:148)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
          :869)
           at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
          rocessConnection(Http11BaseProtocol.java:664)
           at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
          int.java:527)
           at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
          kerThread.java:112)
           at java.lang.Thread.run(Thread.java:595)
          Caused by: java.lang.IllegalStateException: Blobs may not be accessed after seri
          alization
           at org.hibernate.lob.SerializableBlob.getWrappedBlob(SerializableBlob.ja
          va:23)
           at org.hibernate.type.BlobType.set(BlobType.java:38)
           at org.hibernate.type.BlobType.nullSafeSet(BlobType.java:117)
           at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(Abst
          ractEntityPersister.java:1910)
           at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(Abst
          ractEntityPersister.java:1887)
           at org.hibernate.persister.entity.AbstractEntityPersister$1.bindValues(A
          bstractEntityPersister.java:2038)
           at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(Abstr
          actReturningDelegate.java:32)
           at org.hibernate.persister.entity.AbstractEntityPersister.insert(Abstrac
          tEntityPersister.java:2044)
           at org.hibernate.persister.entity.AbstractEntityPersister.insert(Abstrac
          tEntityPersister.java:2481)
           at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentit
          yInsertAction.java:47)
           at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
           at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplic
          ate(AbstractSaveEventListener.java:290)
           at org.hibernate.event.def.AbstractSaveEventListener.performSave(Abstrac
          tSaveEventListener.java:180)
           at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId
          (AbstractSaveEventListener.java:108)
           at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient
          (DefaultPersistEventListener.java:131)
           at org.hibernate.event.def.DefaultPersistEventListener.onPersist(Default
          PersistEventListener.java:87)
           at org.hibernate.event.def.DefaultPersistEventListener.onPersist(Default
          PersistEventListener.java:38)
           at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:620)
           at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:594)
           at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:598)
           at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityMan
          agerImpl.java:186)
           at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(Transact
          ionScopedEntityManager.java:175)
           at com.edieyes.oma.bean.OmaBean.addfile(OmaBean.java:27)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
          java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
          sorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:585)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:112)
           at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationCo
          ntextImpl.java:166)
           at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3Int
          erceptorsInterceptor.java:63)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invok
          e(TransactionScopedEntityManagerInterceptor.java:54)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsI
          nterceptor.java:47)
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
          java:101)
           at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
          
          


          here the code of my servlet

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.PrintWriter;
          import java.sql.Date;
          import java.sql.ResultSet;
          import java.util.Iterator;
          
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.servlet.ServletConfig;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          
          import com.edieyes.oma.bean.Oma;
          import com.edieyes.oma.entities.FichierOma;
          
          
          
          
          public class Test2Servlet extends HttpServlet {
          
           /**
           *
           */
           private static final long serialVersionUID = 1L;
           private Oma usersService;
          
           public void init(ServletConfig config) throws ServletException {
           System.out.println("*******INIT*****");
           try {
           Context ctx = new InitialContext();
           ///usersService = (RemoteUsers) ctx.lookup(RemoteUsers.class.getName());
           usersService = (Oma) ctx.lookup("OmaBean/remote");
          
          
           } catch (NamingException e) {
           System.out.println("**EXCEPTION 1 *");
           // TODO Auto-generated catch block
           e.printStackTrace();
           }
           }
          
          
          
           protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          
           doPost(req,resp);
           }
          
           protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          
           createUsers(req,resp);
          
           }
           private void createUsers(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException
           {
           resp.setContentType("text/html");
           PrintWriter htout = resp.getWriter();
           htout.println("<body><table>");
           htout.println("<tr><th>Id</th><th>Id Client</th><th>numero de commande</th><th>Job</th><th></th></tr>");
          
           System.out.println("***************");
           System.out.println("createUsers");
          
           File file = new File("D:\\Projets\\workspace\\test\\data.dat");
           int fileLength = (int) file.length();
           InputStream value = null;
           FileInputStream fileinp = null;
          
           System.out.println("Inserting data ...\n");
          
           try {
           fileinp = new FileInputStream(file);
           value = (InputStream) fileinp;
           } catch (Exception e) {
           System.out.println(e.getMessage());
          
           }
           usersService.addfile( "Edieyes9","df","moi",new Date(new java.util.Date().getTime()),org.hibernate.Hibernate.createBlob(value,fileLength));
          
          
          
           System.out.println(usersService.findall());
          
          
           }
          
          
          }


          • 2. Re: get Object field in MS SQL
            ccharpentier

            Finally I managed to insert my Blob.

            I need to open the file and do all the stuff in my Bean and not in the servlet like here

            http://docs.jboss.org/ejb3/app-server/tutorial/blob/src/org/jboss/tutorial/blob/bean/LobTesterBean.java