1

This is my first time to learn the SqlResultSetMapping.

My database structure:

table 'Company':

  ID Name
  1  ABC Sdn Bhd
  2  DEF Sdn Bhd
  3  GHI Sdn Bhd

table 'Staff':

staffID staffName companyID 
0001    Ali       1
0002    Abu       2
0003    Ahmad     2
0004    Siti      3
0005    Raju      3

Company.java

@Entity
@Table(name = "company")
@SqlResultSetMapping(name = "COMPANY", entities = 
{
 @EntityResult(entityClass = Company.class, fields = 
 {
  @FieldResult(name = "ID", column = "ID"),
  @FieldResult(name = "name", column = "name")
 }),
 @EntityResult(entityClass = Staff.class, fields = 
 {
  @FieldResult(name = "staffID", column = "staffID"), 
   @FieldResult(name = "staffName", column = "staffName"),
   @FieldResult(name = "companyID", column = "companyID")
 })
 })

@XmlRootElement
@NamedQueries(
{
 @NamedQuery(name = "Company.findAll", 
     query = "SELECT c FROM Company c"
    ),
 @NamedQuery(name = "Company.findById", 
     query = "SELECT c FROM Company c WHERE c.id = :id"
    ),
@NamedQuery(name = "Company.findByName", 
    query = "SELECT c FROM Company c WHERE c.name = :name"
   )
})

public class Company implements Serializable 
{
 @OneToMany(mappedBy = "companyID")
 private Collection<Staff> staffCollection;
 private static final long serialVersionUID = 1L;
 @Id
 @Basic(optional = false)
 @Column(name = "ID")
 private Integer id;
 @Column(name = "name")
 private String name;
 //constructor
 //getters and setters
 ...
}

Staff.java

@Entity
@Table(name = "staff")
@XmlRootElement
@NamedQueries(
{
 @NamedQuery(name = "Staff.findAll", query = "SELECT s FROM Staff s"),
 @NamedQuery(name = "Staff.findByStaffID",
    query = "SELECT s FROM Staff s WHERE s.staffID = :staffID"
   ),
 @NamedQuery(name = "Staff.findByStaffName",
    query = "SELECT s FROM Staff s WHERE s.staffName = :staffName"
   )
})

public class Staff implements Serializable
{
 private static final long serialVersionUID = 1L;
 @Id
 @Basic(optional = false)
 @Column(name = "staffID")
 private String staffID;
 @Column(name = "staffName")
 private String staffName;
 @JoinColumn(name = "companyID", referencedColumnName = "ID")
 @ManyToOne
 private Company companyID;
 //constructor
 //getters and setters
 ...
}

WriteBinaryFile.java:

public class WriteBinaryFile
{
 public static void main(String[] args) 
   throws FileNotFoundException, IOException, ClassNotFoundException
 {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("BinaryFileTestPU");
  EntityManager em = emf.createEntityManager();

  StringBuilder sql = new StringBuilder();
  sql.append("select c.*, s.* ");
  sql.append("from company c ");
  sql.append("join staff s ");
  sql.append("on c.ID = s.companyID");
  Query q = em.createNativeQuery(sql.toString(), "COMPANY");
  List<Company[]> companyList = q.getResultList();

  //---Write object to binary file---
  FileOutputStream fos = new FileOutputStream("test.dat");
  ObjectOutputStream oos = new ObjectOutputStream(fos);

  oos.writeObject(companyList);
  oos.flush();
  oos.close();

  em.close();
  emf.close();
 }
}

ReadBinaryFile.java:

public class ReadBinaryFile
{
 public static void main(String[] args)
   throws FileNotFoundException, IOException, ClassNotFoundException
 {
  //---Read object from binary file---
  FileInputStream fis = new FileInputStream("test.dat");
  ObjectInputStream ois = new ObjectInputStream(fis); 

  List<Company[]> companyList = (List<Company[]>)
  ois.readObject();
  ois.close();

  for(Company[] company: companyList){
  System.out.println(company[0].getName());
  }
 }
}

From the above code, when I read the ReadBinaryFile.java, I get the below errors:

Exception in thread "main" java.lang.ClassCastException:
 binaryFileTest.Company cannot be cast to [LbinaryFileTest.Company;
at binaryFileTest.ReadBinaryFile.main...

What mistake I am doing wrong? How can I solve it?

Reporter
  • 3,897
  • 5
  • 33
  • 47
KKL Michael
  • 795
  • 1
  • 13
  • 31
  • The error itself just says that you get a Company back but try to cast it to an array of Company. Can you check what type you are really writing into the stream? – swinkler May 05 '15 at 09:45
  • @swinkler Which file? – KKL Michael May 05 '15 at 09:48
  • WriteBinaryFile: List companyList = q.getResultList(); is this really a list of arrays of Companies? – swinkler May 05 '15 at 09:50
  • @KKLMichael Do you use an IDE, like Eclipse, Netbeans, Intellij? With there you can debug your code. – Reporter May 05 '15 at 10:02
  • @KKLMichael Please check your code. Removing the scrollbars from your post took a lot of time and maybe some changes has been overwritten. Thanks – Reporter May 05 '15 at 10:06

0 Answers0