3

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

  1. 创建(或连接)数据库
  2. 搜索数据库以查看是否存在具有提供的用户名的用户对象
  3. 如果用户已经存在,则显示对话框消息,否则创建用户

但是,当单击此按钮时,在使用查询对象的结果的行上出现“未找到用户”错误。我很确定这是因为我有一个空数据库,但是我希望代码在程序第一次运行时工作,所以它需要处理一个空数据库。我尝试在第一次运行查询之前更改代码以创建新用户,然后每次单击按钮时代码按我想要的方式工作,并且它可以检测是否需要创建新用户。

我试图创建一个“默认”或“管理员”类型的用户,以便搜索可以工作,但这意味着每次运行程序时都会创建一个重复的“默认”用户,这显然是一个不需要的功能,我可以” t 进行查询以检查数据库是否为空(所以我只能在程序第一次运行时创建默认用户),因为这是我首先遇到的问题!

那么,对于任何使用过 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

谢谢,我也不会存储明文密码,我只是想在开始实施散列密码之前让一个基本的数据库正常工作。

4

1 回答 1

3

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

如果这似乎是原因,要么定义一个持久性单元,要么在查询之前将类引入 ObjectDB,例如:

  em.getMetamodel().entity(User.class);
于 2013-12-24T00:09:15.063 回答