JavaFX和Hibernate-持久保存来自不同屏幕的对象

时间:2019-05-07 17:40:46

标签: java hibernate javafx orm

我是编程方面的新手,现在正在我的第一个项目上工作。我想创建一个应用程序来管理我的财务状况。我在JavaFX中使用GUI,并尝试将其与Hibernate结合使用OR-Mapping来保留我的数据。 首先让我解释一下我如何构建“架构”。

我有POJOs,例如帐户,账簿(金融书),付款(书中列出的付款)。然后,我得到了database类(它就像类之间的控制器一样),在这里我对数据库进行所有操作,例如在财务簿中创建条目或创建新用户/帐户。

最终,我为每个屏幕都设有一个类,例如登录屏幕,概述屏幕... 在我的屏幕类中,例如,在创建新帐户时会调用数据库类中的方法。 从研究中我们知道,您必须通过控制器传递javafx中的对象或参数,才能在另一个屏幕上访问它们。 这是我面临的用例问题,找不到以下解决方案:

  1. 我在注册屏幕上并正在创建一个新用户。
  2. 单击注册按钮后,将使用其给定信息创建用户的对象,然后将我转发到LoginScreen
  3. 该用户的登录名并转发到BookScreen
  4. 现在我有机会创建我的“财务”-图书
  5. 在输入书名并按“添加”按钮时,该书以及用户在数据库中都将保留,并且我将被转发到“概述”屏幕,在其中可以看到书(listview )以及我已支付的所有款项。

您实际上可以在其中看到外观:https://imgur.com/a/8fQBSp6

->现在我的问题是,我不知道如何(通过外键)将我创建的书连接到用户。我的意思是我与用户创建的书应该在书数据库表中将用户的ID作为外键。

我发现我可以轻松地将帐簿添加到帐户的帐簿列表中,并将帐目添加到帐簿中。听起来有点怪异,但这就是我读到的要做的(双向)OneToMany和ManyToOne关系。但是当我尝试这种方法时,我的书要么为空,要么为我的账户。这个问题是javafx所欠的,因为我一次只能将一个对象传递给另一个屏幕。

您可以在github上看到我的整个项目:https://github.com/nailujf7/JF

我的帐户类别:

 @Table(name = "Account") @Entity
    public class Account implements Serializable {

    @OneToMany(mappedBy = "account")
    private List<Book> bookList = new ArrayList<>();

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column (name ="firstname")
    private String firstname;
    @Column (name ="lastname")
    private String lastname;
    @Column (name ="date_of_birth")
    private Date dateOfBirth;
    @Column (name ="username")
    private String username;
    @Column (name ="password")
    private String password;

    public Account(){}

    public Account(String firstname, String lastname, Date dateOfBirth, String username, String password) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.dateOfBirth = dateOfBirth;
        this.username = username;
        this.password = password;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public int getId() {
        return id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

我的图书课:

  @Table(name = "Book")@Entity
    public class Book implements Serializable {

    public Book() {
    }

    public Book(String bookName) {
        this.bookName = bookName;
    }

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private Account account;
    @Column(name = "bookName")
    private String bookName;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "book_id")
    private int book_id;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "payment_id")
    @OrderColumn(name = "index_payment_list")
    private List<Payment> paymentList;


    public int getBook_id() {
        return book_id;
    }

    public void setBook_id(int book_id) {
        this.book_id = book_id;
    }

    public List<Payment> getPaymentList() {
        return paymentList;
    }

    public void setPaymentList(List<Payment> paymentList) {
        this.paymentList = paymentList;
    }


    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

和我的数据库类:

 public class Database {

    private static Session currentSession;
    private static Transaction currentTransaction;
    private Book book;
    private List user;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    private Account account;

    public static Session openCurrentSession() {
        currentSession = getSessionFactory().openSession();
        return currentSession;
    }

    public Session openCurrentSessionwithTransaction() {
        currentSession = getSessionFactory().openSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }

    public void closeCurrentSession() {
        currentSession.close();
    }

    public void closeCurrentSessionwithTransaction() {
        currentTransaction.commit();
        currentSession.close();
    }

    private static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        return configuration.
                addPackage("model").
                addAnnotatedClass(Payment.class).
                addAnnotatedClass(Book.class).
                addAnnotatedClass(Account.class).
                buildSessionFactory(builder.build());
    }

    public Session getCurrentSession() {
        return currentSession;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }

    public Transaction getCurrentTransaction() {
        return currentTransaction;
    }

    public void setCurrentTransaction(Transaction currentTransaction) {
        this.currentTransaction = currentTransaction;
    }


    public boolean checkUserData(String usernamer, String password) {
        account = new Account();
        account.setUsername(usernamer);
        account.setPassword(password);
        String query = "from model.Account acc where acc.username = :username " +
                "and acc.password = :password";
        Query q = openCurrentSession().createQuery(query);
        user = q.setProperties(account).list();
        System.out.println(user.size());
        if (!user.isEmpty()) {
           account=  ((Account) user.iterator().next());
            return true;
        } else {
            return false;
        }
    }

    public void addBook(String bookName) {
        openCurrentSessionwithTransaction();
        book = new Book(bookName);
        getCurrentSession().save(book);
        closeCurrentSessionwithTransaction();
        persistAccountAndBook();

    }

    public void createAccount(String firstname, String lastname, Date date, String username, String password) {
        openCurrentSessionwithTransaction();
        account = new Account(firstname, lastname, date, username, password);
        getCurrentSession().save(account);
        closeCurrentSessionwithTransaction();
    }

    public void persistAccountAndBook(){
        account = new Account("ha","ha",null, "ha","ha");
        account.getBookList().add(book);
        book.setAccount(account);
    }

如果您能得到一些建议,我可能会非常感激,也许可以更改我的体系结构或方法来保留我的数据。 还是您有其他建议来更轻松地访问和持久保存数据? 提前非常感谢您!

0 个答案:

没有答案