使用ObjectDB搜索空用户数据库时出现问题

时间:2013-12-23 22:28:27

标签: java database objectdb

我正在创建一个使用ObjectDB创建和维护一组数据库的java应用程序。我目前正在尝试实现一个DB来存储由用户名和密码字符串组成的用户对象。在JFrame / swing类中,我有一个用于创建新用户的按钮,当单击此按钮时,我希望发生以下情况:

  1. 创建(或连接到)数据库
  2. 搜索数据库以查看是否存在提供用户名的用户对象
  3. 如果用户已存在则显示对话框消息,否则创建用户
  4. 但是,单击此按钮时,我会在使用查询对象结果的行上收到“找不到用户”错误。我很确定这是因为我有一个空的数据库,但是我希望代码在第一次运行程序时工作,所以它需要处理一个空的数据库。我已经尝试在首次运行查询之前更改代码以创建新用户,然后代码按照每次单击按钮时的工作方式运行,并且可以检测是否需要创建新用户。

    我尝试创建一个'默认'或'admin'类型的用户,这样搜索就可以了,但是这意味着每次运行程序时都会创建一个重复的'默认'用户,这显然是一个不需要的功能,并且我无法进行查询以检查数据库是否为空(因此我只能在程序的第一次运行时创建默认用户),因为这是我第一次遇到的问题!

    那么,对于使用ObjectDB的任何人来说,有没有办法处理搜索空数据库的情况?

    以下是我的代码的相关部分:

    public class FrameLogIn extends JFrame {
    
    private JPanel contentPane;
    private String username;
    char[] password;
    private static EntityManager em;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FrameLogIn frame = new FrameLogIn();
                    frame.setVisible(true);
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public FrameLogIn() {
    
        EntityManagerFactory emf = 
                Persistence.createEntityManagerFactory("user.odb");
        em = emf.createEntityManager();
    
    
        final JTextField txtUsername = new JTextField();
        txtUsername.setFont(new Font("Segoe UI Light", Font.PLAIN, 30));
        txtUsername.setHorizontalAlignment(SwingConstants.CENTER);
        txtUsername.setToolTipText("username");
        txtUsername.setText("");
        txtUsername.setBounds(220, 30, 177, 52);
        contentPane.add(txtUsername);
        txtUsername.setColumns(10);
    
        final JPasswordField passwordField = new JPasswordField();
        passwordField.setFont(new Font("Tahoma", Font.PLAIN, 30));
        passwordField.setHorizontalAlignment(SwingConstants.CENTER);
        passwordField.setBounds(220, 93, 177, 52);
        contentPane.add(passwordField);
    
        JButton btnNewUser = new JButton("New user");
        btnNewUser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
    
                username = txtUsername.getText();
                password = passwordField.getPassword();
    
                System.out.println(username);
                TypedQuery<Long> q = em.createQuery(
                        "SELECT COUNT(u) FROM User u "
                        + "WHERE u.name = '" + username + "'", Long.class);
    
                if (q.getSingleResult()>0) {
                    JOptionPane.showMessageDialog(contentPane.getParent(), "A user with this name already exists.");
                } else {
                    em.getTransaction().begin();
                    User newUser = new User(username, password.toString());
                    em.persist(newUser);
                    em.getTransaction().commit();
                    JOptionPane.showMessageDialog(contentPane.getParent(), "User created.");
                }   
            }
        });
        btnNewUser.setFont(new Font("Segoe WP Semibold", Font.PLAIN, 25));
        btnNewUser.setBackground(Color.WHITE);
        btnNewUser.setBounds(71, 175, 149, 63);
        contentPane.add(btnNewUser);
    
    }
    }
    

    和错误:

    Exception in thread "AWT-EventQueue-0" [ObjectDB 2.5.3_03] SELECT COUNT(u) FROM  ==> User <==  u WHERE u.name = ''
    javax.persistence.PersistenceException
    Type User is not found (error 301)
     (position 21)  at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:723)
        at sg.FrameLogIn$2.actionPerformed(FrameLogIn.java:113)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: com.objectdb.o.TEX: Type User is not found
        at com.objectdb.o.MSG.e(MSG.java:107)
        at com.objectdb.o.TRS.g(TRS.java:212)
        at com.objectdb.o.SYR.q(SYR.java:259)
        at com.objectdb.o.SYR.n(SYR.java:188)
        at com.objectdb.o.QRC.<init>(QRC.java:152)
        at com.objectdb.o.QRM.U6(QRM.java:250)
        at com.objectdb.o.MST.U6(MST.java:933)
        at com.objectdb.o.WRA.U6(WRA.java:293)
        at com.objectdb.o.WSM.U6(WSM.java:114)
        at com.objectdb.o.QRR.g(QRR.java:245)
        at com.objectdb.o.QRR.f(QRR.java:154)
        at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:716)
        ... 37 more
    

    谢谢,也是为了记录我不会存储明文密码,我只是想在开始实施散列密码之前让基本数据库工作。

1 个答案:

答案 0 :(得分:3)

如果实体类尚未在数据库中(尚未保留该类的实例),并且未定义持久性单元,则可能发生这种情况。

如果这似乎是原因,请在查询之前定义persistence unit或将类引入ObjectDB,例如:

  em.getMetamodel().entity(User.class);