JPA w / PostgreSQL

servers/defaultServer/server.xml


<server description="Liberty">
<!-- default Config -->
  <featureManager>...</featureManager>
  <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
  <keyStore id="defaultKeyStore" password="${ENCODED_KEYSTORE_PASSWORD}"/>
  <basicRegistry id="basic" realm="BasicRealm">
    <user name="${USERNAME}" password="${ENCODED_PASSWORD}"/>
  </basicRegistry>
  <applicationManager autoExpand="true"/>
  <applicationMonitor updateTrigger="mbean"/>
<!-- JTA Config PostgreSQL -->
  <library id="PostgresLib" filesetRef="PostgresFileset"/>
  <fileset id="PostgresFileset" dir="${DB_HOME}" includes="postgresql-9.4.1212.jre7.jar"/>
  <dataSource id="postgres-jpa" jndiName="jdbc/${DS_NAME}" type="javax.sql.XADataSource">
    <jdbcDriver javax.sql.XADataSource="org.postgresql.xa.PGXADataSource" libraryRef="PostgresLib"/>
    <properties serverName="localhost" port="5432" databaseName="${DB_NAME}" user="${DBUSER}" password="${ENCODED_PASSWORD}"/>
  </dataSource>
<!-- WebApps Config -->
  <webApplication id="${WEB_CONTEXT}" location="${build.war}" name="${WEB_CONTEXT}"/>
</server>

META-INF/persistence.xml


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="${PU_NAME}" transaction-type="JTA">
    <jta-data-source>jdbc/${DS_NAME}</jta-data-source>
    <class>${ENTITY_CLASS}</class>
    <shared-cache-mode>ALL</shared-cache-mode>
    <validation-mode>AUTO</validation-mode>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="none"/>
      <property name="javax.persistence.schema-generation.scripts.action" value="none"/>
    </properties>
  </persistence-unit>
</persistence>

${ENTITY_CLASS.java}


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table (name="${TABLE_NAME}", schema="${SCHEMA_NAME}")
@NamedQUery (name="${CLASSNAME}.findAll", query="SELECT c FROM ${CLASSNAME} c")
public class ${CLASSNAME} implements Serializable {
  @Id
  @GeneratedValue (strategy=GenerationType.AUTO)
  @Column (name="${ID_COLUMN_NAME}", unique=true, nullable=false)
  @Getter @Setter private Integer id;
  @Temporal(TemporalType.DATE)
  @Getter @Setter private Date date;
  @OneToMany (mappedBy="${X}")
  @Getter @Setter private List<${REF_TABLE_CLASS}> list;
}

Recommended Posts

JPA w / PostgreSQL
JPA Basics 1
JPA Basics 2