Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:44:29+00:00 2026-05-31T06:44:29+00:00

I am facing some issues when I write images to the database using JPA.

  • 0

I am facing some issues when I write images to the database using JPA. I am getting below classcastexception. I have attached the code as well.

[EL Warning]: 2012-03-12 
12:02:58.757--UnitOfWork(23191477)--java.lang.ClassCastException: 
java.lang.String cannot be cast to java.lang.Number

Code:

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="Horoscope_Details")
public class HoroscopeDetails {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    public long getId() {
        return id;
    }



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



    public String getStar() {
        return star;
    }



    public void setStar(String star) {
        this.star = star;
    }



    public byte[] getPicture() {
        return picture;
    }



    public void setPicture(byte[] picture) {
        this.picture = picture;
    }



    private String star;

    @Lob @Basic(fetch = FetchType.LAZY)
    @Column(length=1048576) 
    private byte[] picture;





}




package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;

import com.vemanchery.timesheet.emf.EMF;
import com.vemanchery.timesheet.model.HoroscopeDetails;

public class Main2 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // save image into database
        File file = new File("C:\\images\\1.jpg");
        byte[] bFile = null;;
        try {
            bFile = readImageOldWay(file);
        } catch (IOException e1) {

            e1.printStackTrace();
        }

    /*  try {
            FileInputStream fileInputStream = new FileInputStream(file);
            // convert file into array of bytes
            fileInputStream.read(bFile);
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        EntityManager entityManager = EMF.getEntityManager();
        entityManager.getTransaction().begin();
        HoroscopeDetails horoscopeDetails = new HoroscopeDetails();
        horoscopeDetails.setPicture(bFile);
        horoscopeDetails.setStar("lll");
        System.out.println("done!!");
        entityManager.persist(horoscopeDetails);
        entityManager.getTransaction().commit();
        entityManager.flush();
        entityManager.close();

    }

    public static byte[] readImageOldWay(File file) throws IOException {
        Logger.getLogger(Main.class.getName()).log(Level.INFO,
                "[Open File] " + file.getAbsolutePath());
        InputStream is = new FileInputStream(file);
        // Get the size of the file
        long length = file.length();
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

}

I am getting the following exception.

[EL Warning]: 2012-03-12 13:41:33.672--UnitOfWork(23667197)--java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
    at org.eclipse.persistence.sequencing.QuerySequence.updateAndSelectSequence(QuerySequence.java:278)
    at org.eclipse.persistence.sequencing.StandardSequence.getGeneratedVector(StandardSequence.java:71)
    at org.eclipse.persistence.sequencing.DefaultSequence.getGeneratedVector(DefaultSequence.java:163)
    at org.eclipse.persistence.sequencing.Sequence.getGeneratedVector(Sequence.java:257)
    at org.eclipse.persistence.internal.sequencing.SequencingManager$Preallocation_Transaction_NoAccessor_State.getNextValue(SequencingManager.java:468)
    at org.eclipse.persistence.internal.sequencing.SequencingManager.getNextValue(SequencingManager.java:1067)
    at org.eclipse.persistence.internal.sequencing.ClientSessionSequencing.getNextValue(ClientSessionSequencing.java:70)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:349)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:308)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.assignSequenceNumber(UnitOfWorkImpl.java:465)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:4231)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:513)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4176)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:440)
    at main.Main2.main(Main2.java:48)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:44:30+00:00Added an answer on May 31, 2026 at 6:44 am

    java.lang.String cannot be cast to java.lang.Number

    Your mapping could be wrong. You set only one String in your transaction (setStar("lll")). The persistence layer tries to cast this String to a Number. Maybe your column datatype is a number format?

    If the value should not be persisted you should explicitely use the @Transient annotation with the field.

    UPDATE: If you want to know the SQL generated by JPA, add the following to your persistence.xml inside the <persistence-unit> element:

    <properties>
            property name="eclipselink.logging.level.sql" value="FINE" />
    </properties>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing some issues while serializing objects (I am using JBoss Drools, and
Well, since I am facing some issues with OAuth implementation, I decided to go
I'm working using scriptaculous library. However I'm facing some issues with inclusion of the
I am facing some issues in Spring JPA. I successfully configured the Spring JPA
I have to create one expandable /collapseable panel in jQuery, and facing some issues.
Im new to android and im facing some issues... Im using a SurfaceView to
I have a page on iPad and am facing some issues implementing an equivalent
I'm using wamp in my system. I'm facing some issues in my project dicussed
Using WatiN, Im facing some issues with FireFox FF = new FireFox (http://www.google.com); ,
I'm getting ready to implement a source control system (subversion) but I'm facing some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.