テーブルを作成する

Glassfish 4 に Postgis の接続プールを設定する

つぶやきにアクセスする Singleton Session Bean を作る

Entity (com.snail.gmurmur.MurmurTable?)

package com.snail.gmurmur;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.Data;
import org.postgis.PGgeometry;

@Data
@Entity
@Table(name = "murmur_table")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "MurmurTable.findAll", query = "SELECT m FROM MurmurTable m"),
    @NamedQuery(name = "MurmurTable.findById", query = "SELECT m FROM MurmurTable m WHERE m.id = :id"),
    @NamedQuery(name = "MurmurTable.findByWtime", query = "SELECT m FROM MurmurTable m WHERE m.wtime = :wtime"),
    @NamedQuery(name = "MurmurTable.findByWuser", query = "SELECT m FROM MurmurTable m WHERE m.wuser = :wuser"),
    @NamedQuery(name = "MurmurTable.findByMessage", query = "SELECT m FROM MurmurTable m WHERE m.message = :message"),
    @NamedQuery(name = "MurmurTable.findByZoom", query = "SELECT m FROM MurmurTable m WHERE m.zoom = :zoom"),
    @NamedQuery(name = "MurmurTable.findByLocation",
 query = "SELECT m FROM MurmurTable m WHERE FUNCTION('ST_Distance', m.geom, :area) = 0")
})
public class MurmurTable implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Column(name = "wtime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date wtime;
    @Size(max = 255)
    @Column(name = "wuser")
    private String wuser;
    @Size(max = 1023)
    @Column(name = "message")
    private String message;
    @Column(name = "zoom")
    private Integer zoom;
    @Column(name = "geom", columnDefinition = "geometry") // geometry is the type of db column
    @Convert(converter = com.snail.gmurmur.GeometryObjectConverter.class)
    private PGgeometry geom;

    public MurmurTable() {
    }

    public MurmurTable(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof MurmurTable)) {
            return false;
        }
        MurmurTable other = (MurmurTable) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.snail.gmurmur.MurmurTable[ id=" + id + " ]";
    }
}

Converter

FUNCTION 句

RDB 固有の Operator を JPQL から使いたい

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="com_snail_GMurmurPU" transaction-type="JTA">
    <jta-data-source>jdbc/murmur</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
    <properties/>
  </persistence-unit>
  <persistence-unit name="com_snail_GMurmurPU_test" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.snail.gmurmur.MurmurTable</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
    <properties>
      <property name="eclipselink.target-database" value="PostgreSQL"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/demo"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>
      <property name="javax.persistence.jdbc.password" value="postgres"/>
      <!-- OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST,ALL -->
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.logging.timestamp" value="false"/>
      <property name="eclipselink.logging.session" value="false"/>
      <property name="eclipselink.logging.thread" value="false"/>
    </properties>
  </persistence-unit>  
</persistence>

SessionBean? (com.snail.gmurmur.MsgMgrBean?)

package com.snail.gmurmur;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.postgis.LinearRing;
import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon;

@Singleton
@LocalBean
public class MsgMgrBean {

    @PersistenceContext(unitName = "com_snail_GMurmurPU")
    private EntityManager em;

    /**
     * つぶやきを登録する.
     *
     * @param user ユーザ
     * @param message メッセージ
     * @param lon 経度
     * @param lat 緯度
     * @param zoom Zoomレベル
     * @throws SQLException 緯度・経度が不正な場合
     */
    public void registerMurmur(
            final String user, final String message,
            final float lon, final float lat, final int zoom) throws SQLException {

        MurmurTable murmur = new MurmurTable();
        murmur.setWtime(new Date());
        murmur.setWuser(user);
        murmur.setMessage(message);
        murmur.setZoom(zoom);

        PGgeometry geom = new PGgeometry(createPoint(lon, lat));
        murmur.setGeom(geom);

        em.persist(murmur);
    }

    /**
     * 範囲からつぶやきを検索します.
     * @param nwLon 北西経度
     * @param nwLat 北西緯度
     * @param neLon 北東経度
     * @param neLat 北東緯度
     * @param swLon 南西経度
     * @param swLat 南西緯度
     * @param seLon 南東経度
     * @param seLat 南東緯度
     * @return つぶやきのリスト
     */
    public List<MurmurTable> findMurmur(
            final float nwLon, final float nwLat, final float neLon, final float neLat,
            final float swLon, final float swLat, final float seLon, final float seLat) {

        LinearRing ring = new LinearRing(new Point[]{
                    createPoint(nwLon, nwLat), createPoint(neLon, neLat),
                    createPoint(seLon, seLat), createPoint(swLon, swLat),
                    createPoint(nwLon, nwLat)});
        Polygon polygon = new Polygon(new LinearRing[]{ring});
        polygon.setSrid(4326);
        PGgeometry geom = new PGgeometry(polygon);
        
        TypedQuery<MurmurTable> query = em.createNamedQuery("MurmurTable.findByLocation", MurmurTable.class);
        query.setParameter("area", geom);
        
        return query.getResultList();
    }

    /**
     * 座標系4326の Point を作ります.
     * @param lon 経度
     * @param lat 緯度
     * @return Point
     */
    private Point createPoint(final float lon, final float lat) {
        Point point = new Point(lon, lat);
        point.setSrid(4326);
        return point;
    }
}

Test(com.snail.gmurmur.test.MsgWriteTest?)

package com.snail.gmurmur.test;

import com.snail.gmurmur.MsgMgrBean;
import com.snail.gmurmur.MurmurTable;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import mockit.Deencapsulation;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;

public class MsgWriteTest {
    private static EntityManagerFactory emf;
    private static EntityManager em;
    private static MsgMgrBean target = new MsgMgrBean();
 
    @BeforeClass
    public static void setUpClass() {
        emf = Persistence.createEntityManagerFactory("com_snail_GMurmurPU_test");
        em = emf.createEntityManager();
        // Injection
        Deencapsulation.setField(target, "em", em);
    }
 
    @AfterClass
    public static void tearDownClass() {
        em.close();
        emf.close();
    }
    
    @Test
    public void testAddMurmur() throws SQLException {
        em.getTransaction().begin();
        
        Query cleanupQuery = em.createNativeQuery("DELETE FROM murmur_table");
        cleanupQuery.executeUpdate();
        
        target.registerMurmur("test01", "I have a pen", 135.0F, 35.0F, 12);
        
        List<MurmurTable> res = target.findMurmur(130.0F, 40.0F, 140.0F, 40.0F, 130.0F, 30.0F, 140.0F, 30.0F);
        
        assertThat(res.size(), is(equalTo(1)));
        
        MurmurTable m1 = res.get(0);
        
        assertThat(m1.getWuser(), is(equalTo("test01")));
        assertThat(m1.getMessage(), is(equalTo("I have a pen")));
        assertThat(m1.getGeom().toString(), is(equalTo("SRID=4326;POINT(135 35)")));
        
        em.getTransaction().commit();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.snail</groupId>
    <artifactId>GMurmur</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>GMurmur</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jmockit.version>1.1</jmockit.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.googlecode.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>${jmockit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgis</groupId>
            <artifactId>postgis-jdbc</artifactId>
            <version>1.3.3</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.12.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1100-jdbc41</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin> 
                <artifactId>maven-surefire-plugin</artifactId> 
                <version>2.14</version>
                <configuration>
                    <argLine>
-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
                    </argLine>
                </configuration> 
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
            <id>eclipselink</id>
            <layout>default</layout>
            <name>Repository for library EclipseLink (JPA 2.1)</name>
        </repository>
    </repositories>
</project>

Java#Glassfish


添付ファイル: filepool5.png 1904件 [詳細] filepool4.png 2105件 [詳細] filepool3.png 2013件 [詳細] filepool2.png 2054件 [詳細] filepool1.png 2080件 [詳細] filejdbc.png 2156件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-02-27 (木) 01:32:50 (3956d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=31e74200ac
ISBN10
ISBN13
9784061426061