<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    網路冷眼@BlogJava

    熙熙攘攘一閑人 以冷靜的眼光觀察技術
    posts - 88, comments - 193, trackbacks - 0, articles - 28
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    原文地址: http://testwww.netbeans.org/kb/articles/hibernate-javaee.html

    Using Hibernate with the Java Persistence API

    One of the main features of the new Java Persistence API that was introduced with the Java EE 5 platform is that you can plug in any persistence provider that you want. The Sun Java System Application Server 9 ships with Oracles's Toplink persistence provider. In this tutorial, we will install the Hibernate persistence provider on the application server and use it to develop a quick application that accesses a database with the Java Persistence API.

    Software Required for this Tutorial

    Setting up your Environment

    Before we start, we have to register the application server with the IDE and copy over the Hibernate JAR files to the application server's lib folder. We also have to create a library in the IDE for the Hibernate entity manager.

    1. If the application server is not registered in the IDE, register it by choosing Tools > Server Manager and clicking the Add Server button. You can see all registered application servers by opening the Runtime window and expanding the Servers node.
    2. Unzip the Hibernate JAR files to any temporary location and then copy the following JAR files to your application server's lib folder:
      • From Hibernate EntityManager:
        • hibernate-entitymanager.jar
        • lib/hibernate-annotations.jar
        • lib/jboss-archive-browsing.jar
        • lib/javassist.jar
      • From Hibernate Core
        • hibernate3.jar
        • lib/antlr-2.7.6.jar
        • lib/asm-attrs.jar
        • lib/asm.jar
        • lib/c3p0-0.9.0.jar
        • lib/cglib-2.1.3.jar
        • lib/commons-collections-2.1.1.jar
        • lib/commons-logging-1.0.4.jar
        • lib/concurrent-1.3.2.jar
        • lib/dom4j-1.6.1.jar
        • lib/ehcache-1.2.3.jar
        • lib/log4j-1.2.11.jar
    3. Restart the server if it is running. You can restart the server by right-clicking its node in the Runtime window and choosing Restart.
    4. Choose Tools > Library Manager from the main menu. Click New Library, name the library Hibernate, and click OK.
    5. In the Libraries pane of the Library Manager dialog box, select the Hibernate library you just created. Click the Classpath tab and then click Add JAR/Folder and locate the hibernate-entitymanager.jar and javaee.jar in your application server's lib folder. Click Add JAR / Folder and then click OK.

      library manager

    Creating the Entity Classes and Persistence Unit

    Now that we have added the Hibernate libraries to the lib directory of our application server, we can create the entity classes and web interface. We will create a web application named HibernateApp with entity classes and a JSF interface. We will use the NetBeans CRUD facilities.

    1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web category.
    2. Name the project HibernateApp, set the server to Sun Java System Application Server and the Java EE version to Java EE 5. Click Next.
    3. Select the JavaServer Faces framework and click Finish.
    4. In the Projects window, right-click the project node and choose New > Entity Classes from Database. In the Data Source drop-down, select the jdbc/sample database. If prompted for a user name and password, use app and app. Select the CUSTOMER table from the list, click Add and then click Next.

      selecting the tables from the database

    5. Leave the tables and class names as their default, and set the package to hibernatesample.db.

      wizard panel displaying the entity classes that will be generated from the selected tables

    6. Now it is time to create the persistence unit. Click the Create Persistence Unit button. Set HibernateApp as the persistence unit name, select Hibernate as the persistence provider, and select None as the table generation strategy. Click Create.

      create persistence unit dialog box

    7. Click Finish in the Entity Classes from Database wizard. The IDE creates the persistence unit (persistence.xml under Configuration Files node) and the entity classes (under Source Packages).
    8. Finally, let's add some Hibernate-specific properties. Double-click persistence.xml and click the XML button at the top of the editor to switch to the XML view. Add the following property to the persistence unit:
        <persistence-unit name="HibernateApp" transaction-type="JTA">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>jdbc/sample</jta-data-source>
          <properties>
          <property name="hibernate.show_sql" value="true" />
          </properties>
          </persistence-unit>
          </persistence>

      This property causes all SQL statements to be printed to the application server's log file.

    Coding the JSF Interface

    Now we can quickly generate JSF pages for the entity classes with the NetBeans IDE CRUD generation.

    1. Right-click the project node and choose New > JSF Pages from Entity Class. Add both Customer.java and DiscountCode.java to the list of selected classes and click Next.

      New JSF Pages from Entity Class Wizard

    2. Change the package to hibernatesample.controller and click Finish.
    3. To make this application work correctly, we need to make some changes to CustomerController.java and DiscountCodeController.java. We need to modify the create, edit and destroy methods in the files to change the order in which we get the entity manager.

      Open CustomerController.java and DiscountCodeController.java in the Source Editor. Each of the files contains create, edit and destroy methods with the following code:

      EntityManager em = getEntityManager();
          try {
          utx.begin();
          

      Modify the code in each of the methods to change the order in which we get the entity manager.

      EntityManager em = null;
          try {
          utx.begin();
          em = getEntityManager();
          

      For example, the modified code in the create method of CustomerController.java should look like the following (changes in bold):

      public String create() {
          EntityManager em = null;
          try {
          utx.begin();
          em = getEntityManager();
          em.persist(customer);
          utx.commit();
          ...
          

    After creating the JSF pages and modifying the code in the controller classes, you should be able to run and test the application.

    Testing the Project

    Now we need to run the project to test whether the connection to our database is working correctly.

    1. Right-click the project and choose Run Project. The IDE starts the application server, builds and deploys the application, and shows index.jsp in the external web browser.
    2. Click the List of Customer link and you should see the following web page:

      List of Customers JSP Page

    3. Use the New Customer link to create a customer. Then check the server log by right-clicking the application server node in the Runtime window and choosing View Server Log. The SQL statements for creating the customer should be listed in the server log.


    Next Steps

    For more information about using NetBeans IDE 5.5 to develop Java EE applications, see the following resources:

    To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the nbj2ee@netbeans.org mailing list.

    主站蜘蛛池模板: 一二三四视频在线观看中文版免费| 中文字幕永久免费视频| 国产福利视精品永久免费| 亚洲成AV人片在| 久久免费视频99| 午夜影视日本亚洲欧洲精品一区| 东方aⅴ免费观看久久av | 黄网站色视频免费观看45分钟| 全免费a级毛片免费**视频| 亚洲国产欧美日韩精品一区二区三区| 免费无码肉片在线观看| 亚洲欧洲无码AV不卡在线| 国产精品国产午夜免费福利看| 国产精品无码亚洲一区二区三区| 免费人成在线观看播放国产| 国产免费伦精品一区二区三区| 亚洲精品成人片在线播放| 免费看又黄又无码的网站| 亚洲午夜在线播放| 又爽又黄无遮挡高清免费视频 | 成人毛片免费网站| 欧洲亚洲国产精华液| 久久精品国产亚洲Aⅴ蜜臀色欲| 中国性猛交xxxxx免费看| 久久精品国产亚洲AV电影| 一二三四在线观看免费高清中文在线观看 | 四虎永久在线精品视频免费观看| 2022免费国产精品福利在线| 亚洲av无码不卡| 无码高潮少妇毛多水多水免费 | 精品免费视在线观看| 亚洲一区二区三区在线观看蜜桃 | 免费无码又爽又刺激高潮视频| 亚洲国产日韩在线一区| 又黄又爽一线毛片免费观看| 3344在线看片免费| 亚洲精品无播放器在线播放| 精品亚洲综合久久中文字幕| 国产一精品一AV一免费孕妇 | 中文字幕无码免费久久9一区9 | 亚洲国产综合在线|