JPA Configuration, Issues, Pitfalls

JPA Configuration, Issues, Pitfalls

JDBC vs ORM Comparing INGRES-based data access logic Monica Kurth Outline . What is JDBC? . What is ORM? . Advantages and Disadvantages of using JDBC vs ORM . JDBC Configuration, Issues, Pitfalls . JPA Configuration, Issues, Pitfalls . Sample application . Questions? Confidential — © 2008 Ingres Corporation Slide What is JDBC? . Short for Java Database Connectivity . Java API that enables Java programs to execute SQL statements and interact with any SQL-compliant database. Possible to write a single database application that can run on different platforms and interact with different DBMS. Similar to ODBC, but designed specifically for Java Confidential — © 2008 Ingres Corporation Slide Pros and Cons of JDBC Clean and simple SQL processing Good performance with large amounts of data. Large programming overhead Transactions and concurrency must be hand-coded in Awkward way of obtaining and handling connections and the resulting errors SQL Logging is not provided and must be coded specifically (INGRES provides utility!) Confidential — © 2008 Ingres Corporation Slide JDBC Configuration Required Parameters Vendor - connect using the Ingres JDBC driver Hostname - the name or IP address Port - the port to connect to, default is II7 Database - the name of the database User - the user name to connect with Password - the password Confidential — © 2008 Ingres Corporation Slide JDBC Code Sample [more code] //Instantiate Driver class Class.forName("com.ingres.jdbc.IngresDriver"); //Create Connection Connection connection = DriverManager.getConnection( jdbc:ingres://localhost:II7/demodb, USERNAME, PASSWORD); //Create SQL statement Statement statement = connection.createStatement(); //Execute SQL statement.execute(“SELECT * FROM Employee”); //Tidy up resources – this will need some exception catching etc… statement.close(); connection.close(); [more code] Confidential — © 2008 Ingres Corporation Slide JDBC Configuration – Notes . Leave username and password blank for a local database. The Ingres driver version must be the one provided by the local Ingres installation; look for the iijdbc.jar file under <Ingres dir>\IngresII\ingres\lib . Place iijdbc.jar on the classpath . For SQL Logging, place iijdbc.properties on the classpath to enable Ingres JDBC driver logging. Confidential — © 2008 Ingres Corporation Slide Ingres JDBC logging On Windows, this will log to c:\tmp\iijdbc.log . iijdbc.properties file on classpath Folder must exist - it will not be created! ingres.jdbc.trace.log=/tmp/iijdbc.log ingres.jdbc.trace.timestamp=true ingres.jdbc.trace.drv=5 ingres.jdbc.trace.ds=5 Set ingres.jdbc.trace.msg=5 level 1 (errors) ingres.jdbc.trace.nl=3 to level 5 (low level calls) Read up more detail on: http://docs.ingres.com/connectivity/JDBCTracing Confidential — © 2008 Ingres Corporation Slide What is ORM? . Short for Object Relational Mapping . Lets business code access objects rather than DB tables . Hides details of SQL queries from OO logic . Makes it easy to extend ORM-based applications . Based on JDBC ‘under the hood’ Confidential — © 2008 Ingres Corporation Slide Pro ORM No need to deal with the database implementation Little need to write SQL statements = code reduction. Simple configuration Entities based on business concepts rather than database structure Easy navigation of the object graph Confidential — © 2008 Ingres Corporation Slide Pro ORM (continued) Concurrency support - No coding necessary although locking can be manually configured if desired Excellent caching built in - e.g. Hibernate 2 levels of caching, detailed configuration possible. Can greatly increase application performance! Transaction management and automatic key generation Very good configurable logging built in Confidential — © 2008 Ingres Corporation Slide Against ORM Can be slow with large Inserts & Updates (batch processing) Initially steep learning curve Sometimes hard to track and optimize Confidential — © 2008 Ingres Corporation Slide ORM - JPA - Hibernate . Most ORM tools in the Java world are based on JDBC . Connection configuration easy and similar to JDBC . JPA - first common standard released in May 2006 as part of the Java Standard and Java Enterprise Edition 1.5 . Based largely on Hibernate . Hibernate still exceeds JPA functionality in some areas and remains most popular JPA implementation Confidential — © 2008 Ingres Corporation Slide Other JPA implementations . OpenJPA – an Apache JPA implementation . Apache Cayenne – another Apache JPA project . JPOX – Open Source JPA implementation . TopLink – Oracle’s JPA implementation . Kodo – BEA’s JPA flavour . Eclipse Link – recently started project for INGRES Integration – contributions welcome! Confidential — © 2008 Ingres Corporation Slide JPA Configuration XML schema <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence Provider: http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd“ version="1.0"> Hibernate <persistence-unit name="jpa-ingres"> Automatically find persistence <provider>org.hibernate.ejb.HibernatePersistence</provider> objects in all Java <properties> classes <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> Log all SQL <property name="hibernate.connection.driver_class" statements value="com.ingres.jdbc.IngresDriver"/> Use Ingres Driver <property name="hibernate.connection.url" value="jdbc:ingres://localhost:II7/ Use performancedb"/> Connect to DB using JDBC String Hibernate’s <property name="hibernate.dialect" value="org.hibernate.dialect.IngresDialect"/> Ingres dialect <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> update the DB schema only other options: validate – create - create-drop Confidential — © 2008 Ingres Corporation Slide JPA Configuration - Notes . More than one persistence unit can be defined to cater for multiple databases etc. Changes only in configuration file . Actual code not touched by implementation . SQL Logging is easily configured . Hibernate implementation needs a number of Jar files on the classpath Confidential — © 2008 Ingres Corporation Slide Hibernate log4j configuration (similar for other JPA implementations) log4j.jar and log4j.properties must be placed on classpath log4j.appender.FileAppender=org.apache.log4j.FileAppender Define File Logger log4j.appender.FileAppender.File=c:/test.log log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.FileAppender.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L - %m%n log4j.appender.stdout=org.apache.log4j.ConsoleAppender Define Console Logger log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L - %m%n log4j.rootLogger=DEBUG, FileAppender, stdout List required Loggers log4j.logger.org.hibernate=ERROR log4j.logger.uk.co.luminary.jdbc=DEBUG log4j.logger.org.apache.commons.collections=ERROR Set logging levels for different packages in the application and Hibernate. Allowed are: FATAL, ERROR, WARN, INFO, DEBUG Confidential — © 2008 Ingres Corporation Slide JPA entity configuration (independent of JPA implementation) package uk.co.luminary.entity; public Employee(Integer id) import javax.persistence.*; { this.id = id; @Entity tell the JPA this is a mapped entity } @Table(name=“EMP_TABLE") Optional: table name; //the usual getters and setters public class Employee class name = default { public Integer getId() //define the primary key { return this.id; @Id the entity’s ID @GeneratedValue } (strategy=GenerationType.AUTO) private Integer id; public void setId(Integer id) { let the DB use it’s default this.id = id; private String firstName; id generation strategy private String surName; } (in INGRES, this is private Date birthdate; SEQUENCE) private String position; public String getFirstName() private String notes; { return this.firstName; //default constructor } public Employee(){ } [MORE CODE] Confidential — © 2008 Ingres Corporation Slide Hibernate – deleting an entity Example log statements DEBUG - Initializing object from ResultSet: [uk.co.luminary.entity.Employee#21] DEBUG - Hydrating entity: [uk.co.luminary.entity.Employee#21] DEBUG - returning '1960-02-10 00:00:00' as column: birthdate2_0_ DEBUG - returning 'John' as column: firstName2_0_ DEBUG - returning 'on holiday until August' as column: notes2_0_ DEBUG - returning 'Developer' as column: position2_0_ DEBUG - returning 'Smith' as column: surName2_0_ DEBUG - done processing result set (1 rows) DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1) DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) DEBUG - closing statement DEBUG - total objects hydrated: 1 DEBUG - resolving associations for [uk.co.luminary.entity.Employee#21] DEBUG - done materializing entity [uk.co.luminary.entity.Employee#21] DEBUG - initializing non-lazy collections DEBUG - done entity load DEBUG - deleting a persistent instance DEBUG - deleting [uk.co.luminary.entity.Employee#21] DEBUG - Flushed: 0 insertions, 0 updates, 1 deletions to 1 objects DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections DEBUG - listing entities: DEBUG - uk.co.luminary.entity.Employee{position=Developer, notes=on holiday until August, surName=Smith, firstName=John, birthdate=1960-02-10 00:00:00, id=21} DEBUG - executing flush DEBUG - registering flush begin

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    31 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us