Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Exception when read Geometry type from Postgis(Basically, how access Postgis Geometry type useing EclipseLink )
Exception when read Geometry type from Postgis [message #1697537] Thu, 04 June 2015 16:09
jiyao weng is currently offline jiyao wengFriend
Messages: 1
Registered: June 2015
Junior Member
Hello,

I am using EclipseLink (JPA 2.1) on Glassfish to read a column with type of Geometry from Postgis database. The table Country simply has column shape (Geometry) and id (Integer)

Following is entity class of Country
import com.vividsolutions.jts.geom.Geometry;

@Entity
@Table(name = "country")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Country.findAll", query = "SELECT c FROM Country c"),
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Column(name = "shape")
private Geometry shape;

public Country() {
}

public Integer getId() {
return id;
}

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


public Geometry getShape() {
return shape;
}

public void setShape(Geometry shape) {
this.shape = shape;
}

}

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
getEntityManager().persist(entity);
}

public void edit(T entity) {
getEntityManager().merge(entity);
}

public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}

public T find(Object id) {
return getEntityManager().find(entityClass, id);
}

public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}

public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}

public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}

@Stateless
public class CountryFacade extends AbstractFacade<Country> {
@PersistenceContext(unitName = "com.j_mavenproject2-ejb_ejb_1.0-SNAPSHOTPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
return em;
}

public CountryFacade() {
super(Country.class);
}
}

following is the code to read shape field from Country table:

List<Country> countries;
countries = countryFacade.findAll();

for(int i = 0; i < 10; i++) {
country = countries.get(i);
Geometry geometry = country.getShape();

LOG.log(Level.INFO, "geometry = {0}", geometry.toText());

}

Thank you very much.
Previous Topic:Two Persists within One Transactional Method
Next Topic:@cache
Goto Forum:
  


Current Time: Thu Mar 04 12:45:43 GMT 2021

Powered by FUDForum. Page generated in 0.01584 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top