JPA J2SE 桌面应用范例

JPA虽然是Java EE规范的一部分,但是可以在J2SE环境下单独使用,不需要Web相关容器。本篇主要通过Netbeans 自动生成的代码来学习JPA相关内容。

1.打开Netbeans 8.1 添加Mysql数据库连接

2.新建Java应用程式,输入项目名称JpaExample

3.在项目中右键新建其他,选择主样例窗体/详细样例窗体,输入类名称和包名称

4.选择第一步建立的数据库连接->选择表(user),右侧为程式中用到的栏位,默认是全部带到右边,不需要的可以移到左边

5.选择在Swing界面上显示的栏位,默认全部在右边表示全选,不需要的可以移到左边。点击完成

6.查看生成的类,persistence.xml是jpa配置文件,User.java是对于table的实体,MasterDetailForm.java是自动生成的Swing界面,自动添加的库文件包括实现JPA2.1 的EclipseLink和添加连接mysql数据库的jdbc驱动,都是安装Netbeans自带的可以自行替换指定版本。

7.运行,点击New就可以新增内容,Delete删除上面Jtable选中的数据,Save保存到数据库,Refresh 刷新数据,主要是数据库数据与JPA同步,如果数据库有变动,刷新后本地JPA显示变动后数据。

8.代码分析,完整MasterDetailForm.java如下。

package com.cc.jpa;

import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author dev
 */
public class MasterDetailForm extends JPanel {

    public MasterDetailForm() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("ccPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT u FROM User u");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        idLabel = new javax.swing.JLabel();
        usernmaeLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        usernmaeField = new javax.swing.JTextField();
        passwordField = new javax.swing.JTextField();
        nameField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${usernmae}"));
        columnBinding.setColumnName("Usernmae");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${password}"));
        columnBinding.setColumnName("Password");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Name");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);

        masterScrollPane.setViewportView(masterTable);

        idLabel.setText("Id:");

        usernmaeLabel.setText("Usernmae:");

        passwordLabel.setText("Password:");

        nameLabel.setText("Name:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.id}"), idField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("null");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), idField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.usernmae}"), usernmaeField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("null");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), usernmaeField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.password}"), passwordField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("null");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), passwordField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.name}"), nameField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("null");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), nameField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        saveButton.setText("Save");
        saveButton.addActionListener(formListener);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(formListener);

        newButton.setText("New");
        newButton.addActionListener(formListener);

        deleteButton.setText("Delete");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(newButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(refreshButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idLabel)
                    .addComponent(usernmaeLabel)
                    .addComponent(passwordLabel)
                    .addComponent(nameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idField, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(usernmaeField, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteButton, newButton, refreshButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLabel)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernmaeLabel)
                    .addComponent(usernmaeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(refreshButton)
                    .addComponent(deleteButton)
                    .addComponent(newButton))
                .addContainerGap())
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == saveButton) {
                MasterDetailForm.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                MasterDetailForm.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                MasterDetailForm.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton) {
                MasterDetailForm.this.deleteButtonActionPerformed(evt);
            }
        }
    }// </editor-fold>                        

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
    }                                             

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
        int[] selected = masterTable.getSelectedRows();
        List<com.cc.jpa.User> toRemove = new ArrayList<com.cc.jpa.User>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            com.cc.jpa.User u = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(u);
            entityManager.remove(u);
        }
        list.removeAll(toRemove);
    }                                            

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {
        com.cc.jpa.User u = new com.cc.jpa.User();
        entityManager.persist(u);
        list.add(u);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
    }                                         

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<com.cc.jpa.User> merged = new ArrayList<com.cc.jpa.User>(list.size());
            for (com.cc.jpa.User u : list) {
                merged.add(entityManager.merge(u));
            }
            list.clear();
            list.addAll(merged);
        }
    }                                          

    // Variables declaration - do not modify
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel idLabel;
    private java.util.List<com.cc.jpa.User> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton newButton;
    private javax.swing.JTextField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField usernmaeField;
    private javax.swing.JLabel usernmaeLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration
    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(MasterDetailForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterDetailForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterDetailForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterDetailForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new MasterDetailForm());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}

主要部分:

entityManager初始化,ccPU是在persistence.xml中定义的persistence-unit 的名称, query 和list的 代码 一个是新建一个查询,另一个是查出所有资料。query其实可以用另一种写法,使用entityManager.createNamedQuery("User.findAll");

查看User.java文件会发现,Netbeans 生成的文件里面默认生成了几个Query,也可以根据需要自行添加,集中管理。

entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("ccPU").createEntityManager();
query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT u FROM User u");
list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());

User:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cc.jpa;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author dev
 */
@Entity
@Table(name = "user", catalog = "cc", schema = "")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
    @NamedQuery(name = "User.findByUsernmae", query = "SELECT u FROM User u WHERE u.usernmae = :usernmae"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
    @NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name")})
public class User implements Serializable {

    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "usernmae")
    private String usernmae;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;

    public User() {
    }

    public User(Integer id) {
        this.id = id;
    }

    public User(Integer id, String usernmae, String password, String name) {
        this.id = id;
        this.usernmae = usernmae;
        this.password = password;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        Integer oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public String getUsernmae() {
        return usernmae;
    }

    public void setUsernmae(String usernmae) {
        String oldUsernmae = this.usernmae;
        this.usernmae = usernmae;
        changeSupport.firePropertyChange("usernmae", oldUsernmae, usernmae);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        String oldPassword = this.password;
        this.password = password;
        changeSupport.firePropertyChange("password", oldPassword, password);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        changeSupport.firePropertyChange("name", oldName, name);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won‘t work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.cc.jpa.User[ id=" + id + " ]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}

New,主要是新增一个新的User对象,没有写到数据库中,主要是前端操作

com.cc.jpa.User u = new com.cc.jpa.User();
entityManager.persist(u);
list.add(u);
int row = list.size() - 1;
masterTable.setRowSelectionInterval(row, row);
masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));

Delete,执行entityManager.remove(u)并没有写到数据库是因为有开启Transaction ,在保存的时候才会commit

int[] selected = masterTable.getSelectedRows(); List<com.cc.jpa.User> toRemove = new ArrayList<com.cc.jpa.User>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            com.cc.jpa.User u = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(u);
            entityManager.remove(u);
        }
        list.removeAll(toRemove);

Save,主要就是Transaction 的提交以及异常处理

try {
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
} catch (RollbackException rex) {
rex.printStackTrace();
entityManager.getTransaction().begin();
List<com.cc.jpa.User> merged = new ArrayList<com.cc.jpa.User>(list.size());
for (com.cc.jpa.User u : list) {
merged.add(entityManager.merge(u));
}
list.clear();
list.addAll(merged);
}

Refresh,同步数据库数据

entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
java.util.Collection data = query.getResultList();
for (Object entity : data) {
    entityManager.refresh(entity);
}
list.clear();
list.addAll(data);

此范例主要是J2SE 下JPA的使用,实际项目中,可能设计很多table,程式也会相应的分层,如果只是学习一下JPA的特性熟悉下方法,这种方式也是没问题的。使用eclipse 创建JPA时 EclipseLink 需要单独下载,EclipseLink 前身是Oracle TopLink ,以前商业框架 ADF的一部分,用JPA 选择EclipseLink ,Hibernate 或其他实现都没有关系,而且只用JPA的功能,不用实现提供的一些扩展的话,切换使用都是没有问题的,就像JSP页面用不同的容器,Tomcat、Jetty、GlassFish结果是没差别的。

时间: 2024-10-17 23:50:44

JPA J2SE 桌面应用范例的相关文章

java学习笔记——韩顺平视频

第0讲:开山篇 1.  Java se J2se        桌面 Java ee J2ee        网络 Java me J2me        手机 2.  SQL server   Mysql   Oracle 3. 学习软件编程的注意事项 1.高效愉快学习 2.先建立一个整体框架然后细节 3.用什么再学习什么 4.先知道how,再知道why 5.软件编程是一门"做中学"学科,做了才会 6.适当囫囵吞枣 7.琢磨别人怎么做,不是我认为这么做 第1讲  内容介绍.项目演示.

Java从入门到精通(一)

Java编程可以分为三个方向 ① Java se (j2se) 桌面开发 ② Java ee (j2ee) WEB开发 ③ Java me (j2me) 手机开发 java se 是基础中的基础 Java se 课程介绍 Java 面向对象编程 Java图形界面开发(swing) Java数据库编程 Java文件I/O流 Java网络编程(山寨版的QQ) Java多线程(坦克大战) Java的编程架构

JAVA入门到精通-第0讲

p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; font-size: 10.5pt; font-family: "Calibri", "sans-serif" } .MsoChpDefault { } div.Section1 { } java平台 1.J2SE java开发平台标准版 2.J2EE java开发平台企

Spring,JPA与Hibernate的最新整合范例视频

和大家分享我最新制作的视频课程:Spring,JPA与Hibernate的最新整合范例视频讲解(含源代码),视频教程观看网址: http://www.javathinker.net/lesson.jsp?type=3&number=15 在网页上:选择[课程下载]链接,就能下载源代码,本节课的源代码在chapter24目录下. 本视频的配套书籍是我写的<精通JPA与Hibernate:Java对象持久化技术详解>第3版,已经完稿.预计2020年出版. 原文地址:https://blog

JPA常用注解

JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.        JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分.但它不囿于EJB 3.0,你可以在Web应用.甚至桌面应用中使用.JPA的宗旨是为POJO提供持久化标准规范.        JPA的总体思想和现有Hibernate.TopLink,JDO等ORM框架大体一致.总的来说,JPA包括以下3方面的技术:     

jpa概述

1.1.  JPA 是什么 JavaPersistence API,用于对象持久化的API,是Java EE 5.0 平台标准的 ORM 规范,使得应用程序以统一的方式访问持久层. JPA 是EJB3 EntityBean,是一套从EJB3.0核心标准中分离出来的独立的标准文档,J2EE 4规范中最为人所熟悉的用来处理数据持久的Entity Bean,在Java EE5中被推到重来,取而代之的是java开发的通用持久化规范Java Persistence API 1.0, 其实就是完全重新定义了

杏彩娱乐Java的不同版本:J2SE、J2EE、J2ME的区别

杏彩娱乐1998年12月,SUN公司发布了Java 1.2,开始使用"Java 2" 这一名称,目前我们已经很少使用1.2之前的版本,所以通常所说的Java都是指Java2. Java 有三个版本,分别为 J2SE.J2EE和J2ME,以下是详细介绍. J2SE(Java 2 Platform Standard Edition) 标准版 J2SE是Java的标准版,主要用于开发客户端(桌面应用软件),例如常用的文本编辑器.下载软件.即时通讯工具等,都可以通过J2SE实现. J2SE包含

《J2EE,J2SE,J2ME》

J2EE(Java EE)是做企业级应用的.比如,163邮箱,比如某公司的管理系统 J2SE(Java SE)就是Java语言的标准版本,类似于C++,主要做桌面软件,比如Eclipse,MyEclipse由于微软的封杀,java的桌面应用非常少且本身没有太大的优势却劣势很大,不过J2SE应该是学习Java的人首先应该学习的. J2ME就是手机或者嵌入式软件,不是android,是以前那种手机的java应用 作者:武井 链接:http://www.zhihu.com/question/20827

J2SE总结

刚刚学完J2SE,总结一下,不当之处敬请赐教. 基本概念: 刚刚接触Java,突然冒出了好多了名词,一会儿J2SE,一会儿Java,一会儿JDK,蒙圈了,总结一下他们的关系吧. SDK:Software Development Kit,软件开发工具: JDK:Java Development Kit,Java开发工具: J2SDK:Java2 SDK: Java2:自JDK1.2之后Java版本统称为Java2(点击查看JDK各版本): J2SE.J2EE.J2ME是根据Java的应用而开发的不