Reputation: 535
I'm trying to receive a list of objects of type Tempfiles
and I'm not sure what has gone wrong in the way of fetching it. and when I receive the list, the size of the list is not zero but when I try to use it, I'm getting a ClassCastException :
Here's the pojo of the class Tempfiles
:
@Entity
@Table(name="tempfiles")
@NamedQuery(name="Tempfile.findAll", query="SELECT t FROM Tempfile t")
public class Tempfile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer tempfilesid;
private byte[] filestream;
//bi-directional many-to-one association to Filemetadata
@ManyToOne
@JoinColumn(name="documentid")
private Filemetadata filemetadata;
public Tempfile() {
}
public Integer getTempfilesid() {
return this.tempfilesid;
}
public void setTempfilesid(Integer tempfilesid) {
this.tempfilesid = tempfilesid;
}
public byte[] getFilestream() {
return this.filestream;
}
public void setFilestream(byte[] filestream) {
this.filestream = filestream;
}
public Filemetadata getFilemetadata() {
return this.filemetadata;
}
public void setFilemetadata(Filemetadata filemetadata) {
this.filemetadata = filemetadata;
}
}
Here's the method that I use to :
public List getTempFileData()
{
String METHOD_NAME = "getTempFileData";
logger.logEntering(METHOD_NAME);
String strQueryToGetStream = "select t.tempfilesid, t.filemetadata.documentid, t.filestream from Tempfile t";
logger.logInfo(strQueryToGetStream);
Query query = entityManager.createQuery(strQueryToGetStream);
List <Tempfile>tempFileList = query.getResultList();
logger.logExiting(METHOD_NAME);
return tempFileList;
}
Here's how I receive it :
List <Tempfile>tempFileList = fileDao.getTempFileData();
And I get the exception when I use the list in a loop :
for(Tempfile tempFile : tempFileList)
{
decryptAndEncryptStream(tempFile, job, publicKey);
}
And this is the exception I'm getting :
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.global.empris.domain.Tempfile
Would greatly appreciate your help and thanks in advance.
Upvotes: 0
Views: 63
Reputation: 633
IMO, you're getting an array of objects from JPA, because you only selected a portion of your entity. If you want your entity, select it and fetch its children collections like so
select t from Tempfile t left join fetch t.filestream
Upvotes: 0
Reputation: 5393
The statement List <Tempfile>tempFileList = query.getResultList();
returns a List<Object>
. You should us Transformers.aliasToBean(TempFile.class)
Upvotes: 0
Reputation: 73568
Your query is returning a list of Object[]
instead of TempFile
. You are only choosing specific columns, so it won't be mapped to the full object.
Process each Object[]
like you would a resultset, or change your query to select a full object (i.e. SELECT t FROM Tempfile t
).
Upvotes: 1