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)