PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Play 2.2 con Hibernate JPA e Postgres

Hai detto di non aver scritto alcun codice, così ho deciso di mostrarti come ho creato una nuova applicazione Play! 2.2 con JPA e PostgreSQL. Puoi fare lo stesso e verificare la differenza.

Per prima cosa ho creato una nuova applicazione Play con il comando:

play new testApp

Poi ho creato il file persistence.xml nella directory testApp/conf/META-INF e l'ho riempito con questo contenuto:

<persistence xmlns="https://java.sun.com/xml/ns/persistence"
  xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0">
  <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.format_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

Ho aggiunto al mio testApp/conf/application.conf:

jpa.default=defaultPersistenceUnit
db.default.driver=org.postgresql.Driver
db.default.url="postgres://postgres:example@sqldat.com/test"
db.default.jndiName=DefaultDS

Ho anche creato una classe model di esempio:

@Entity
@SequenceGenerator(name = "Token_generator", sequenceName = "test_sequence", allocationSize = 1, initialValue = 1)
public class Test {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Token_generator")
  public Long id;
  public String name;
}

Ho avviato l'app Play con il comando:

play ~run

A quel punto sono riuscito a vedere il sito web funzionante all'indirizzo https://localhost:9000/ e anche la nuova tabella test nel database test di Postgres.