JPQL Join和类强制转换异常

时间:2017-08-29 12:50:44

标签: spring-boot spring-data-jpa

我在春季启动时使用JPA和Hibernate。

我有两个jpa实体

@Entity
@Table(name="courses_type")
public class CoursesType implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    @Column(name="course_id")
    private int courseId;

    @Column(name="level")
    private int level;

    private int credential;

    private String status;

    private String type;

    @Column(name="updated_by")
    private int updatedBy;

    @Column(name="updated_on")
    private Timestamp updatedOn;

    @ManyToOne(fetch=FetchType.LAZY, optional=false)
    @JoinColumn(name="credential", referencedColumnName="value_id",insertable=false,updatable=false)
    @Where(clause="status='live'")
    private BaseAttributeList credentialData;


    @ManyToOne(fetch=FetchType.LAZY, optional=false)
    @JoinColumn(name="level", referencedColumnName="value_id",insertable=false,updatable=false)
    @Where(clause="status='live'")
    private BaseAttributeList courseLevelData;

... setters and getters

}

第二实体

@Entity
@Table(name="attribute_list")
public class AttributeList implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="value_id")
    private int valueId;

    private String status;

    @Column(name="value_name")
    private String valueName;



}

现在我正在尝试在CourseTypeRepo中编写JPQL查询

@Query("Select sct.courseId, sct.credential, sct.credentialData from CoursesType"
            + " sct where sct.courseId IN(?1) and sct.status = ?2")
    List<CoursesType> getDataWithAttributes1(ArrayList<Integer> courseId, String status);

现在,当我迭代结果时,我得到的类Cast <<异常

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.domain.CoursesType

基本上我试图使用一个jpql查询获取完整数据。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您不想获取完整对象但只想获取其某些属性,则必须提供projection,然后在查询方法中使用它,例如:

public class GameController2 extends JFrame implements MouseListener, MouseMotionListener, ComponentListener{
    public JLabel CoordsLabel;
    public JLabel buttonWA;
    public JLabel buttonW;
    public JLabel buttonWD;
    public JLabel buttonA;
    public JLabel buttonD;
    public JLabel buttonAS;
    public JLabel buttonS;
    public JLabel buttonSD;
    public JFrame controlFrame;
    public int mMovedX;
    public int mMovedY;
    public Robot robot;
    public ImageIcon cIconOver, cIcon, rIcon, rIconOver, brIcon, brIconOver, bIcon, bIconOver, blIcon, blIconOver, lIcon, lIconOver, tlIcon, tlIconOver, tIcon, tIconOver, trIcon, trIconOver;
    public int fWidth;
    public int fHeight;

    public ArrayList<JLabel> buttons;
    public int gX = 0;
    public int gY = 0;

    public Icon origImg;

    public GameController2(){
        controlFrame = new JFrame();
        GridBagLayout gridbag = new GridBagLayout();
        controlFrame.setLayout(gridbag);
        controlFrame.setSize(300,300);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;

        tlIcon = new ImageIcon("tlIconTest.png");
        buttonWA = new JLabel();
        buttonWA.setName("buttonWA");
        //buttonWA.setBackground(Color.BLACK);
        buttonWA.setOpaque(true);
        buttonWA.addMouseListener(this);
        buttonWA.addMouseMotionListener(this);
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        gridbag.setConstraints(buttonWA, c);
        controlFrame.add(buttonWA);
        buttonWA.setIcon(tlIcon);

        tIcon = new ImageIcon("tIconTest.png");
        buttonW = new JLabel("W");
        buttonW.setName("buttonW");
        buttonW.setBackground(Color.BLACK);
        buttonW.setOpaque(true);
        buttonW.addMouseListener(this);
        buttonW.addMouseMotionListener(this);
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(buttonW, c);
        controlFrame.add(buttonW);
        buttonW.setIcon(tIcon);

        trIcon = new ImageIcon("trIconTest.png");
        buttonWD = new JLabel("W+D");
        buttonWD.setName("buttonWD");
        buttonWD.setBackground(Color.BLACK);
        buttonWD.setOpaque(true);
        buttonWD.addMouseListener(this);
        buttonWD.addMouseMotionListener(this);
        c.gridx = 2;
        c.gridy = 0;
        gridbag.setConstraints(buttonWD, c);
        controlFrame.add(buttonWD);
        buttonWD.setIcon(trIcon);

        lIcon = new ImageIcon("lIconTest.png");
        buttonA = new JLabel("A");
        buttonA.setName("buttonA");
        buttonA.setBackground(Color.BLACK);
        buttonA.setOpaque(true);
        buttonA.addMouseListener(this);
        buttonA.addMouseMotionListener(this);
        c.gridx = 0;
        c.gridy = 1;
        gridbag.setConstraints(buttonA, c);
        controlFrame.add(buttonA);
        buttonA.setIcon(lIcon);

        cIcon = new ImageIcon("rMouseOrig.png");
        CoordsLabel = new JLabel();
        CoordsLabel.setName("CoordsLabel");
        //CoordsLabel.setBackground(Color.BLACK);
        CoordsLabel.setOpaque(true);
        CoordsLabel.addMouseListener(this);
        CoordsLabel.addMouseMotionListener(this);
        c.gridx = 1;
        c.gridy = 1;
        gridbag.setConstraints(CoordsLabel, c);
        controlFrame.add(CoordsLabel);
        CoordsLabel.setIcon(cIcon);

        rIcon = new ImageIcon("rIconTest.png");
        buttonD = new JLabel();
        buttonD.setBackground(Color.BLACK);
        buttonD.setOpaque(true);
        buttonD.setName("buttonD");
        buttonD.addMouseListener(this);
        buttonD.addMouseMotionListener(this);
        c.gridx = 2;
        c.gridy = 1;
        gridbag.setConstraints(buttonD, c);
        controlFrame.add(buttonD);
        buttonD.setIcon(rIcon);

        blIcon = new ImageIcon("blIconTest.png");
        buttonAS = new JLabel();
        buttonAS.setBackground(Color.BLACK);
        buttonAS.setOpaque(true);
        buttonAS.setName("buttonAS");
        buttonAS.addMouseListener(this);
        buttonAS.addMouseMotionListener(this);
        c.gridx = 0;
        c.gridy = 2;
        gridbag.setConstraints(buttonAS, c);
        controlFrame.add(buttonAS);
        buttonAS.setIcon(blIcon);

        bIcon = new ImageIcon("bIconTest.png");
        buttonS = new JLabel("S");
        buttonS.setName("buttonS");
        buttonS.setBackground(Color.BLACK);
        buttonS.setOpaque(true);
        buttonS.addMouseListener(this);
        buttonS.addMouseMotionListener(this);
        c.gridx = 1;
        c.gridy = 2;
        gridbag.setConstraints(buttonS, c);
        controlFrame.add(buttonS);
        buttonS.setIcon(bIcon);

        brIcon = new ImageIcon("brIconTest.png");
        buttonSD = new JLabel("");
        buttonSD.setBackground(Color.BLACK);
        buttonSD.setOpaque(true);
        buttonSD.setName("buttonSD");
        buttonSD.addMouseListener(this);
        buttonSD.addMouseMotionListener(this);
        c.gridx = 2;
        c.gridy = 2;
        gridbag.setConstraints(buttonSD, c);
        //controlFrame.add(buttonSD);
        buttonSD.setIcon(brIcon);

        controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        controlFrame.setVisible(true);
        controlFrame.setFocusableWindowState(false);

        rIconOver = new ImageIcon("rIconOverTest.png");
        brIconOver = new ImageIcon("brIconOverTest.png");
        bIconOver = new ImageIcon("bIconOverTest.png");
        blIconOver = new ImageIcon("blIconOverTest.png");
        lIconOver = new ImageIcon("lIconOverTest.png");
        tlIconOver = new ImageIcon("tlIconOverTest.png");
        tIconOver = new ImageIcon("tIconOverTest.png");
        trIconOver = new ImageIcon("trIconOverTest.png");
        cIconOver = new ImageIcon("rMouseOver.png");

        try{
            robot = new Robot();
        } catch (AWTException e){
            e.printStackTrace();
        }
    }

    @Override
    public void componentResized(ComponentEvent e){
//
    }

    @Override
    public void componentHidden(ComponentEvent e){
        //
    }

    @Override
    public void componentShown(ComponentEvent e){
        //
    }

    @Override
    public void componentMoved(ComponentEvent e){
        //
    }

    @Override
    public void mouseEntered(MouseEvent e){
            JLabel label = (JLabel)e.getSource();
            String name = label.getName();

            if(name == "buttonSD"){
                robot.keyPress(KeyEvent.VK_S);
                robot.keyPress(KeyEvent.VK_D);
                origImg = buttonSD.getIcon();
                buttonSD.setIcon(brIconOver);
            } else if(name == "buttonS"){
                robot.keyPress(KeyEvent.VK_S);
                origImg = buttonS.getIcon();
                buttonS.setIcon(bIconOver);
            } else if (name == "buttonWD"){
                robot.keyPress(KeyEvent.VK_D);
                robot.keyPress(KeyEvent.VK_W);
                origImg = buttonWD.getIcon();
                buttonWD.setIcon(trIconOver);
            } else if(name == "buttonW"){
                robot.keyPress(KeyEvent.VK_W);
                origImg = buttonW.getIcon();
                buttonW.setIcon(tIconOver);
            } else if(name == "buttonWA"){
                robot.keyPress(KeyEvent.VK_W);
                robot.keyPress(KeyEvent.VK_A);
                origImg = buttonWA.getIcon();
                buttonWA.setIcon(tlIconOver);
            }else if(name == "buttonA"){
                robot.keyPress(KeyEvent.VK_A);
                origImg = buttonA.getIcon();
                buttonA.setIcon(lIconOver);
            } else if(name == "buttonAS"){
                robot.keyPress(KeyEvent.VK_A);
                robot.keyPress(KeyEvent.VK_S);
                origImg = buttonAS.getIcon();
                buttonAS.setIcon(blIconOver);
            } else if(name == "buttonD"){
                robot.keyPress(KeyEvent.VK_D);
                origImg = buttonD.getIcon();
                buttonD.setIcon(rIconOver);
            } else if(name == "CoordsLabel"){
                robot.keyRelease(KeyEvent.VK_A);
                robot.keyRelease(KeyEvent.VK_W);
                robot.keyRelease(KeyEvent.VK_S);
                robot.keyRelease(KeyEvent.VK_D);
            }

              System.out.println(label);
        }

    @Override
        public void mouseMoved(MouseEvent m){
        //
    }

        @Override
    public void mouseDragged(MouseEvent e){
        //
    }

    @Override
    public void mouseExited(MouseEvent e){
        robot.keyRelease(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_W);
        robot.keyRelease(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_D);
        if((JLabel)e.getSource() != CoordsLabel){
            JLabel tmpLabel = (JLabel)e.getSource();
            tmpLabel.setIcon(origImg);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e){
        robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

    }

    @Override
    public void mousePressed(MouseEvent e){
        robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
    }

    @Override
    public void mouseClicked(MouseEvent e){
        //
    }

    public static void main(String args[]){
        new GameController2();
    }
}

要使这个技巧有效,你必须在查询中使用别名......