There are some similar questions, but none of them could help me. I tried to fill a JTable with data from a DB using hybernate and everething was ok when I extracted information from a single table, but when I tried to do the same with joined tables I started getting an exception. This are some of my pojo and mapping files(they are generated by NetBeans)
Console.WriteLine(TaxPayer.Heading);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
And this is the main part
public class Schedule implements java.io.Serializable {
private Integer scheduleId;
private Days days;
private Groups groups;
private Lessons lessons;
private Rooms rooms;
private Teachers teachers;
//constructors, getters and setters
}
public class Groups implements java.io.Serializable {
private Integer groupId;
private String groupName;
private Set schedules = new HashSet(0);
//constructors, getters and setters
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 03.10.2015 17:16:15 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="university.pojo.Schedule" table="schedule" catalog="university" optimistic-lock="version">
<id name="scheduleId" type="java.lang.Integer">
<column name="schedule_id" />
<generator class="identity" />
</id>
<many-to-one name="days" class="university.pojo.Days" fetch="select">
<column name="day_id" not-null="true" />
</many-to-one>
<many-to-one name="groups" class="university.pojo.Groups" fetch="select">
<column name="group_id" not-null="true" />
</many-to-one>
<many-to-one name="lessons" class="university.pojo.Lessons" fetch="select">
<column name="lesson_id" not-null="true" />
</many-to-one>
<many-to-one name="rooms" class="university.pojo.Rooms" fetch="select">
<column name="room_id" not-null="true" />
</many-to-one>
<many-to-one name="teachers" class="university.pojo.Teachers" fetch="select">
<column name="teacher_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 03.10.2015 17:16:15 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="university.pojo.Groups" table="groups" catalog="university" optimistic-lock="version">
<id name="groupId" type="java.lang.Integer">
<column name="group_id" />
<generator class="identity" />
</id>
<property name="groupName" type="string">
<column name="group_name" length="100" not-null="true" />
</property>
<set name="schedules" table="schedule" inverse="true" lazy="true" fetch="select">
<key>
<column name="group_id" not-null="true" />
</key>
<one-to-many class="university.pojo.Schedule" />
</set>
</class>
</hibernate-mapping>
I used this tutuorial as a guidline for writing my program, changing it were necessary. https://netbeans.org/kb/docs/java/hibernate-java-se.html
This is the exact exception message
package university.ui;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import university.pojo.Schedule;
import university.pojo.Teachers;
import university.util.HibernateUtil;
public class uni extends javax.swing.JFrame {
public uni() {
initComponents();
}
private static String QUERY_FOR_TEACHER="from Teachers t where t.teacherName like '";
private static String QUERY_FOR_TEACHER_SCHEDULE="from Schedule sd "
+ "join sd.lessons sdl "
+ "join sd.rooms sdr "
+ "join sd.groups sdg "
+ "join sd.teachers sdt "
+ "join sd.days sdd "
+ "where sdd.dayName like '";
private void runQueryForTeacherSchedule(){
executeHQLQuery(QUERY_FOR_TEACHER_SCHEDULE + "mondey" + "' "
+ "and sdt.teacherName like '" + "bruce" + "'",2);
}
private void runQueryBasedOnTeacherName() {
executeHQLQuery(QUERY_FOR_TEACHER + enterTeacherName.getText() + "%'",1);
}
private void executeHQLQuery(String hql, int sc) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery(hql);
List resultList = q.list();
switch(sc){
//displayResult(resultList,sc);
case 1: displayResult1(resultList);
break;
case 2: displayResult2(resultList);
break;
}
session.getTransaction().commit();
} catch (HibernateException he) {
he.printStackTrace();
}
}
private void displayResult1(List resultList) {
Vector<String> tableHeaders = new Vector<String>();
Vector tableData = new Vector();
tableHeaders.add("Names");
for(Object o : resultList) {
Teachers teachers = (Teachers)o;
Vector<Object> oneRow = new Vector<>();
oneRow.add(teachers.getTeacherName());
tableData.add(oneRow);
}
teacherNameTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}
private void displayResult2(List resultList) {
Vector<String> tableHeaders = new Vector<String>();
Vector tableData = new Vector();
//tableHeaders.add("Room");
tableHeaders.add("Group");
//tableHeaders.add("Start");
//tableHeaders.add("End");
for(Object o : resultList) {
Schedule schedule = (Schedule)o;
Vector<Object> oneRow = new Vector<>();
//oneRow.add(schedule.getRooms().getRoomNumber());
oneRow.add(schedule.getGroups().getGroupName());
//oneRow.add(schedule.getLessons().getLessonStart());
//oneRow.add(schedule.getLessons().getLessonEnd());
tableData.add(oneRow);
}
teacherScheduleTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTabbedPane2 = new javax.swing.JTabbedPane();
jPanel4 = new javax.swing.JPanel();
enterTeacherName = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
teacherNameTable = new javax.swing.JTable();
teacherWorkDay = new javax.swing.JComboBox();
jScrollPane4 = new javax.swing.JScrollPane();
teacherScheduleTable = new javax.swing.JTable();
jPanel5 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jPanel6 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
enterTeacherName.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(javax.swing.event.CaretEvent evt) {
enterTeacherNameCaretUpdate(evt);
}
});
teacherNameTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null},
{null},
{null},
{null}
},
new String [] {
"Names"
}
));
/*
teacherNameTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() == 1) {
final JTable target = (JTable)e.getSource();
final int row = target.getSelectedRow();
final int column = target.getSelectedColumn();
// Cast to ur Object type
String urObjctInCell = (String)target.getValueAt(row, column);
}
}
});
*/
teacherNameTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
teacherNameTableMouseClicked(evt);
}
});
jScrollPane3.setViewportView(teacherNameTable);
teacherWorkDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mondey", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" }));
teacherScheduleTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Room", "Group", "Start", "End"
}
));
jScrollPane4.setViewportView(teacherScheduleTable);
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(enterTeacherName, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(teacherWorkDay, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(enterTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(teacherWorkDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap())
);
jTabbedPane2.addTab("Teachers", jPanel4);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null},
{null},
{null},
{null}
},
new String [] {
"Title 1"
}
));
jScrollPane1.setViewportView(jTable1);
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null},
{null},
{null},
{null}
},
new String [] {
"Title 1"
}
));
jScrollPane2.setViewportView(jTable2);
javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel5Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 345, Short.MAX_VALUE))
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jScrollPane2)
);
jTabbedPane2.addTab("tab2", jPanel5);
javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
jPanel6.setLayout(jPanel6Layout);
jPanel6Layout.setHorizontalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 555, Short.MAX_VALUE)
);
jPanel6Layout.setVerticalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 427, Short.MAX_VALUE)
);
jTabbedPane2.addTab("tab3", jPanel6);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane2)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane2)
);
pack();
}// </editor-fold>
private void enterTeacherNameCaretUpdate(javax.swing.event.CaretEvent evt) {
runQueryBasedOnTeacherName();
}
private void teacherNameTableMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
//int row=teacherNameTable.getSelectedRow();
//String tableClick=(teacherNameTable.getModel().getValueAt(row, 0).toString());
runQueryForTeacherSchedule();
}
/**/
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(uni.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(uni.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(uni.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(uni.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new uni().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField enterTeacherName;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTabbedPane jTabbedPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable teacherNameTable;
private javax.swing.JTable teacherScheduleTable;
private javax.swing.JComboBox teacherWorkDay;
// End of variables declaration
}
答案 0 :(得分:0)
您在university.ui.uni.runQueryForTeacherSchedule(uni.java:28)
因为这会运行一个查询进行连接,然后该查询返回List<Objects[]>
这就是它抛出异常的原因
ClassCastException: [Ljava.lang.Object; cannot be cast to university.pojo.Schedule
其中Ljava.lang.Object
是对象数组。
如果你想教老师那么
将查询更改为
private static String QUERY_FOR_TEACHER_SCHEDULE="select sd from Schedule sd "
+ "join sd.lessons sdl "
+ "join sd.rooms sdr "
+ "join sd.groups sdg "
+ "join sd.teachers sdt "
+ "join sd.days sdd "
+ "where sdd.dayName like '";
现在这将返回Schedule Object。