Postgre SQLHibernate Module
Postgre SQLHibernate Module
Module Objectives!
Developers: DBAs:
“this database “quit sending all that
is a dog” inefficient SQL”
About Hibernate
Application
Tier Persistent
Objects
Hibernate Transactions Connections Sessions Queries Criteria
Data
Tier
© 2011 EnterpriseDB. All rights reserved. 7
RDBMS History!
1970 Edgar Codd of IBM
• Wrote many papers to solve the searchable data problem for Database
Management Systems!
• Eventually a paper one called:!
– “A Relational Model of Data for Large Shared Data Banks”!
1973 to 1979 Stonebraker and Eugene Wong created project
INGRES based on Codd’s paper
• Created at U. Cal Berkeley!
1978 onward Many of the members of INGRES become part of
RDBMS companies such as Sybase , Illustra, Informix
1978 Larry Ellison releases first version of Oracle based on
System R
© 2011 EnterpriseDB. All rights reserved. 9
RDBMS History Continued!
In 1986 Michael Stonebraker led the development of
PostgreSQL at Berkley
– Stands for “Post Ingres”
Worked on it in Academia for 8 years
Found life in the Open Source Community in the mid
1990’s
Application Server
Application
A
Persistent
Objects
Database
Hibernate can be used in two forms!
Hibernate Classic API
• Relational tables are bound to Java classes Java Classes
via XML Mapping file
• Columns are bound to Class fields via XML Hibernate Hibernate via
Mapping file Classic JPA
• Keeps the Java Code cleaner an easier to
read.
Hibernate Core
Hibernate with java.persistence API
• A standard for doing Java ORM JDBC
• Mapping of Tables to Classes is done via
Java annotations
• It is a standard and other implementations
exist
– Eclipse Link
– Open JPA
Our Schema for a Car Rental Shop!
@Entity!
@Table(name = "cars", catalog = "carrentals", schema = "")!
public class Car implements Serializable {!
@Id !
@Column(name = "car_id")!
@GeneratedValue(strategy = GenerationType.AUTO)!
private String carId;!
@Column(name = "description")!
private String description;!
@Column(name = "duration")!
private Object duration;!
@Entity!
@Table(name = ”rentals", catalog = "carrentals", schema = "")!
public class Rental implements Serializable {!
@Id !
@Column(name = ”rental_id")!
private String carId;!
@OneToOne!
@JoinColumn(name=“car_id”)!
private Car car;!
@OneToOne!
@JoinColumn(name=“customer_id”);!
private Customer customer;!
@Entity
public class Employee {
@Id private int id;
private String name;
@ManyToMany
@JoinTable(name=“EMP_PROJ”,
joinColumns=@JoinColumn(name=“EMP_ID”),
inverseJoinColumns(name=“PROJ_ID”))
private Collection<Project> projects;
Programming with Hibernate!
/* Code Snipet to list all the cars. */
import java.util.List;
import org.hibernate.Session;
import javaapplication3.Car;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
…….
/* Always start with a session : Sessions are single threaded units of work */
List cars = session.createQuery("from Car").list(); /* Hibernate use the HQL Query language */
session.getTransaction().commit();
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/devel</property>
<property name="hibernate.connection.username">devel</property>
<property name="hibernate.connection.password”>devel</property>
</session-factory>
</hibernate-configuration>
Development Configuration Options!
IDE Choices App. Server Databases
Choices
NetBeans Bundled
Hibernate Libraries
Java 1.6
Ways to do an O-R Project with Hibernate!
Select Hibernate Classic vs. JPA
Decide on appropriate approach:
1. Work with existing relational schema and build persistent
Java Classes
2. Have classes auto-generated from database (JBOSS
Tools)
3. Start with Persistent classes let Hibernate Create the
tables
What can happen – What did happen!
Lab 1!
Install NetBeans
Connect to Postgres
• See Lab Instructions
Create a connection to PostgreSQL
Open Existing Hibernate Project JavaApplicaton3
• Run against database
Common Issues with Postgres and
Hibernate
Hibernate and PostgreSQL!
fsync
fsync
Updates are Causing Havoc!
• The update statements send all of the columns even if they did
not change
– The Postgres logs show the following lines when just
updating the column “filler” to “HelloWorld”
@Entity
@Table(name = "tellers")
@org.hibernate.annotations.Entity(
selectBeforeUpdate = true,
dynamicInsert = true, dynamicUpdate = true)
@NamedQueries(
{@NamedQuery(name = "Tellers.findAll",
query = "SELECT t FROM Tellers t"),
@NamedQuery(name = "Tellers.findByTid",
query = "SELECT t FROM Tellers t WHERE t.tid = :tid"),
@NamedQuery(name = "Tellers.findByBid",
query = "SELECT t FROM Tellers t WHERE t.bid = :bid"),
@NamedQuery(name = "Tellers.findByTbalance",
query = "SELECT t FROM Tellers t
WHERE t.tbalance = :tbalance")})
public class Tellers implements Serializable {
Replication!
• Replicate using Slony to additional
servers
– Asynchronous single
master multiple slave
• Load Balance reads using pgpool-II
• All writes go to the master node
• Connection pooling is also handled by
pgpool-II
The problem!
CREATE TABLE tab ( id INTEGER PRIMARY KEY, somefield text NOT NULL );
INSERT INTO tab(id, somefield) VALUES (1,'fred');
THEN
-- Row is being updated by an application that does not know
-- about row versioning. It's changed data in the row, but hasn't
-- incremented the version. We'll do that for it. NEW.rowversion :=
NEW.rowversion + 1;
END IF;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
Trades
Mar. 2012
Jan. 2012
Apr. 2012
Table is a child of the
Trades Table
Not very scalable. Two inserts and 1 delete for every intert.
Professional Training
• Learn PostgreSQL from the experts
• Web and on-site training
• Training for developers and DBAs
Postgres Plus Standard Server!
Advanced Security
• Built-in SQL firewall
• Stored code obfuscation
Advanced Manageability
• Automatic software updates
• Effortless patch management
Production Support
• 24x7, around-the-clock support
• Help from the Postgres experts
Postgres Plus Advanced Server!
Advanced Performance Oracle Compatibility Advanced Scalability
• Infinite Cache/memory caching • PL/SQL compatible • Infinite Cache/memory caching
• High-speed, parallel loader • Data dictionary views • High-speed, parallel loader
• Bulk collect/bind • Pro*C support • Bulk collect/bind
• Advanced performance monitoring • Migration tools
Trainings Certifications