Servlet NewJavaServlet Test



Yüklə 11,16 Mb.
Pdf görüntüsü
səhifə96/97
tarix07.11.2018
ölçüsü11,16 Mb.
#78896
1   ...   89   90   91   92   93   94   95   96   97

CHAPTER 12 
■ 
DATABASES AND JYTHON: OBJECT RELATIONAL MAPPING AND USING JDBC 
 
255 
 
   
username
 
   
password
 
     
   
1
 
     
   
org.hibernate.dialect.PostgreSQLDialect
 
     
   
 
Our next step is to code the plain old Java object for our database table. In this case, we’ll code an 
object named Player that contains only four database columns: id, first, last, and position. As you’ll see, 
we use standard public accessor methods with private variables in this class. 
Listing 12-32. 
package org.jythonbook.entity; 
 
public class Player { 
 
    public Player(){} 
 
    private long id; 
    private String first; 
    private String last; 
    private String position
 
    public long getId(){ 
        return this.id; 
    } 
 
    private void setId(long id){ 
        this.id = id; 
    } 
 
    public String getFirst(){ 
        return this.first; 
    } 
 
    public void setFirst(String first){ 
        this.first = first; 
    } 
 
    public String getLast(){ 
        return this.last; 
    } 
 
    public void setLast(String last){ 
        this.last = last; 
    } 
 
    public String getPosition(){ 
        return this.position; 
www.it-ebooks.info


CHAPTER 12 

 DATABASES AND JYTHON: OBJECT RELATIONAL MAPPING AND USING JDBC 
 
256 
 
    } 
 
    public void setPosition(String position){ 
        this.position = position; 
    } 
 

Lastly, we will create a configuration file that will be used by Hibernate to map our POJO to the 
database table itself. We’ll ensure that the primary key value is always populated by using a generator 
class type of increment. Hibernate also allows for the use of other generators, including sequences if 
desired. The player.hbm.xml file should go into the same package as our POJO, in this case, the 
org.jythonbook.entity package. 
Listing 12-33. Creating a Hibernate Configuration File 
 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
package="org.jythonbook.entity"> 
 
     
        Player for Hockey Team 
 
         
             
       
 
 
       
 
       
 
       
 
 
   
 
 
 
 
That is all we have to do inside of the Java project for our simple example. Of course, you can add as 
many entity classes as you’d like to your own project. The main point to remember is that all of the 
entity classes are coded in Java, and we will code the rest of the application in Jython. 
Jython Implementation Using the Java Entity Classes 
The remainder of our use-case will be coded in Jython. Although all of the Hibernate configuration files 
and entity classes are coded and place within the Java project, we’ll need to import that project into the 
Jython project, and also import the Hibernate JAR file so that we can make use of its database session 
and transactional utilities to work with the entities. In the case of Netbeans, you’d create a Python 
application then set the Python platform to Jython 2.5.0. After that, you should add all of the required 
Hibernate JAR files as well as the Java project JAR file to the Python path from within the project 
properties. Once you’ve set up the project and taken care of the dependencies, you’re ready to code the 
implementation. 
www.it-ebooks.info


CHAPTER 12 
■ 
DATABASES AND JYTHON: OBJECT RELATIONAL MAPPING AND USING JDBC 
 
257 
 
As said previously, for this example we are coding a hockey roster implementation. The application 
runs on the command line and basically allows one to add players to a roster, remove players, and check 
the current roster. All of the database transactions will make use of the Player entity we coded in our Java 
application, and we’ll make use of Hibernate’s transaction management from within our Jython code. 
Listing 12-34. Hockey Roster Application Code 
from org.hibernate.cfg import Environment 
from org.hibernate.cfg import Configuration 
from org.hibernate import Query 
from org.hibernate import Session 
from org.hibernate import SessionFactory 
from org.hibernate import Transaction 
from org.jythonbook.entity import Player 
 
 
class HockeyRoster: 
 
    def __init__(self): 
        self.cfg = Configuration().configure() 
        self.factory = self.cfg.buildSessionFactory() 
 
    def make_selection(self): 
        ''' 
        Creates a selector for our application.  The function prints output to the 
        command line.  It then takes a parameter as keyboard input at the command 
        line in order to choose our application option. 
        ''' 
        options_dict = {1:self.add_player, 
                            2:self.print_roster, 
                            3:self.search_roster, 
                            4:self.remove_player} 
        print "Please chose an option\n" 
 
        selection = raw_input('''Press 1 to add a player, 2 to print the roster, 
                                  3 to search for a player on the team, 
                                  4 to remove player, 5 to quit: ''') 
        if int(selection) not in options_dict.keys(): 
                if int(selection) == 5: 
                    print "Thanks for using the HockeyRoster application." 
                else: 
                    print "Not a valid option, please try again\n" 
                    self.make_selection() 
        else: 
            func = options_dict[int(selection)] 
            if func: 
                func() 
            else: 
                print "Thanks for using the HockeyRoster application." 
 
    def add_player(self): 
        ''' 
        Accepts keyboard input to add a player object to the roster list. 
www.it-ebooks.info


Yüklə 11,16 Mb.

Dostları ilə paylaş:
1   ...   89   90   91   92   93   94   95   96   97




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə