home / blog

Postgresql hello world – part 3

So how about some JPA? First get a JEE eclipse build, and install JPA tools and eclipse link. Next download a postgres JDBC driver, eclipse JPA implementation and OpenJPA – JPA support for J2SE.

You should now be able to use the New… Other… JPA… “Entities from tables” option. Create a database connection and enter username/password etc. and the JDBC driver jar etc.

Here we’ve created an entity for Horse and Rider.

@Entity
@Table(name = "horses")
public class Horse {

    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "size")
    private int size;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "horses_riders", joinColumns = @JoinColumn(name = "hid"), inverseJoinColumns = @JoinColumn(name = "rid"))
    private Collection<Rider> riders;

    public Horse() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Collection<Rider> getRiders() {
        return riders;
    }
}
@Entity
@Table(name = "riders")
public class Rider {
    @Id
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "horses_riders", joinColumns = @JoinColumn(name = "rid"), inverseJoinColumns = @JoinColumn(name = "hid"))
    private Collection horses;

    public Rider() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<Horse> getHorses() {
        return horses;
    }
}

A fairly basic setup would be as follows:

    EntityManagerFactory factory = Persistence.createEntityManagerFactory(
            "horses", System.getProperties());

    EntityManager em = factory.createEntityManager();

    Query horseQuery = em.createQuery("SELECT h FROM Horse h");

    for (Horse horse : (List<Horse>) horseQuery.getResultList()) {
        System.out.println(horse.getName() + horse.getRiders());
    }

The META-INF/persistence.xml file defines the entities and postgres connection info.

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<persistence-unit name="horses">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>

<class>hellojpa.Horse</class>
<class>hellojpa.Rider</class>

<properties>

<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/horsedb" />

<property name="openjpa.ConnectionUserName" value="horse" />
<property name="openjpa.ConnectionPassword" value="password" />

<property name="openjpa.DynamicEnhancementAgent" value="true" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>

</persistence>
This entry was posted in geek. Bookmark the permalink.

Leave a Reply

Your email address will not be published.