这是我第一次学习SqlResultSetMapping
.
我的数据库结构:
表“公司”:
ID Name
1 ABC Sdn Bhd
2 DEF Sdn Bhd
3 GHI Sdn Bhd
表“员工”:
staffID staffName companyID
0001 Ali 1
0002 Abu 2
0003 Ahmad 2
0004 Siti 3
0005 Raju 3
公司.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
...
}
工作人员.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
...
}
写二进制文件.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();
}
}
读取二进制文件.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());
}
}
}
从上面的代码中,当我阅读 ReadBinaryFile.java 时,我得到以下错误:
Exception in thread "main" java.lang.ClassCastException:
binaryFileTest.Company cannot be cast to [LbinaryFileTest.Company;
at binaryFileTest.ReadBinaryFile.main...
我做错了什么?我该如何解决?