How to work with PGpoint for Geolocation using PostgreSQL? - postgresql

I found a lot of answers that suggest to use spatial data with Hibernate spatial data geolocation but I want to know if that is the best because I found that PostgreSQL works with PGpoint for GeoLocation. I implemented but it doesn't work because doesn't save.
ERROR: column "location" is of type point but expression is of type character varying
I have the same question but nobody answered him. So let me add other question below if nobody knows about this question.
As suggestion I'd want to know what is the best way to use Geo data on Spring Boot Context
Thanks! have a good day.

There is no way to save/update/get/ PGpoint object directly,
Then you have to create your own user type for supporting PGpoint in order to convert it, before this is saved, UserType is a class of Hibernate which allows to create custom type in order to convert it before to save on database.
Here is code that you need to implement:
First: Need to create a class that implements of UserType:
public class PGPointType implements UserType {
#Override
public int[] sqlTypes() {
return new int[]
{
Types.VARCHAR
};
}
#SuppressWarnings("rawtypes")
#Override
public Class<PGpoint> returnedClass() {
return PGpoint.class;
}
#Override
public boolean equals(Object obj, Object obj1) {
return ObjectUtils.equals(obj, obj1);
}
#Override
public int hashCode(Object obj) {
return obj.hashCode();
}
#Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException {
if (names.length == 1) {
if (resultSet.wasNull() || resultSet.getObject(names[0]) == null) {
return null;
} else {
return new PGpoint(resultSet.getObject(names[0]).toString());
}
}
return null;
}
#Override
public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException {
if (value == null) {
statement.setNull(index, Types.OTHER);
} else {
statement.setObject(index, value, Types.OTHER);
}
}
#Override
public Object deepCopy(Object obj) {
return obj;
}
#Override
public boolean isMutable() {
return Boolean.FALSE;
}
#Override
public Serializable disassemble(Object obj) {
return (Serializable) obj;
}
#Override
public Object assemble(Serializable serializable, Object obj) {
return serializable;
}
#Override
public Object replace(Object obj, Object obj1, Object obj2) {
return obj;
}
}
Second: Need to add on entity header #TypeDef annotation, add a name and the PGPointType that you created it and on some field header of type PGpoint, add #Type annotation with the name that you created it:
#TypeDef(name = "type", typeClass = PGPointType.class)
#Entity
public class Entity {
#Type(type = "type")
private PGpoint pgPoint;
// Getters and setters
}
Kind regards.

Related

Simplest way to migrate from older JPA to newer with version with Query.getResultList() returning List<Object[]> instead of List<Vector>

We're currently researching the best way to upgrade from Toplink 2.1-60f to EclipseLink 2.6. The project is somewhat large and most of the manual work would have to be done in parts of the code where we are using NativeQuery. Query.getResultList() result differs between the two JPA-implementations as TopLink returns a List<Vector> and EclipseLink on the other hand returns a List<Object[]>. The code is unfortunately therefore littered with List<Vector> references.
Part of the solution would be to convert the result from list array to a list of vectors. Instead of doing this in all the numerous places manually, I was thinking we could use AspectJ to intercept the getResultList() calls and convert the return values. Is this a viable solution? Has anyone implemented similar solutions? We're using Maven as our build tool.
Thanks in advance!
My suggestion is: Use a good IDE and refactor your code!
But because you asked for an AOP solution, here is a self-consistent AspectJ example. As I have never used JPA, I will just recreate your situation as a little abstraction.
Abstract Query base implementation with lots of dummy methods:
package de.scrum_master.persistence;
import java.util.*;
import javax.persistence.*;
public abstract class MyBaseQueryImpl implements Query {
#Override public int executeUpdate() { return 0; }
#Override public int getFirstResult() { return 0; }
#Override public FlushModeType getFlushMode() { return null; }
#Override public Map<String, Object> getHints() { return null; }
#Override public LockModeType getLockMode() { return null; }
#Override public int getMaxResults() { return 0; }
#Override public Parameter<?> getParameter(String arg0) { return null; }
#Override public Parameter<?> getParameter(int arg0) { return null; }
#Override public <T> Parameter<T> getParameter(String arg0, Class<T> arg1) { return null; }
#Override public <T> Parameter<T> getParameter(int arg0, Class<T> arg1) { return null; }
#Override public <T> T getParameterValue(Parameter<T> arg0) { return null; }
#Override public Object getParameterValue(String arg0) { return null; }
#Override public Object getParameterValue(int arg0) { return null; }
#Override public Set<Parameter<?>> getParameters() { return null; }
#Override public Object getSingleResult() { return null; }
#Override public boolean isBound(Parameter<?> arg0) { return false; }
#Override public Query setFirstResult(int arg0) { return null; }
#Override public Query setFlushMode(FlushModeType arg0) { return null; }
#Override public Query setHint(String arg0, Object arg1) { return null; }
#Override public Query setLockMode(LockModeType arg0) { return null; }
#Override public Query setMaxResults(int arg0) { return null; }
#Override public <T> Query setParameter(Parameter<T> arg0, T arg1) { return null; }
#Override public Query setParameter(String arg0, Object arg1) { return null; }
#Override public Query setParameter(int arg0, Object arg1) { return null; }
#Override public Query setParameter(Parameter<Calendar> arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(Parameter<Date> arg0, Date arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(String arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(String arg0, Date arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(int arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(int arg0, Date arg1, TemporalType arg2) { return null; }
#Override public <T> T unwrap(Class<T> arg0) { return null; }
}
The only method missing is getResultList(), so now let us provide two different implementations for it, extending the abstract base implementation:
Concrete Query implementation returning List<Vector>:
This emulates your TopLink class.
package de.scrum_master.persistence;
import java.util.*;
public class VectorQuery extends MyBaseQueryImpl {
#Override
public List getResultList() {
List<Vector<String>> resultList = new ArrayList<>();
Vector<String> result = new Vector<>();
result.add("foo"); result.add("bar");
resultList.add(result);
result = new Vector<>();
result.add("one"); result.add("two");
resultList.add(result);
return resultList;
}
}
Concrete Query implementation returning List<Object[]>:
This emulates your EclipseLink class.
package de.scrum_master.persistence;
import java.util.*;
public class ArrayQuery extends MyBaseQueryImpl {
#Override
public List getResultList() {
List<Object[]> resultList = new ArrayList<>();
Object[] result = new Object[] { "foo", "bar" };
resultList.add(result);
result = new Object[] { "one", "two" };
resultList.add(result);
return resultList;
}
}
Driver application:
The application creates queries of both concrete subtypes, each time assuming that the list elements will be vectors.
package de.scrum_master.app;
import java.util.*;
import de.scrum_master.persistence.*;
public class Application {
public static void main(String[] args) {
List<Vector<?>> resultList;
resultList = new VectorQuery().getResultList();
for (Vector<?> result : resultList)
System.out.println(result);
resultList = new ArrayQuery().getResultList();
for (Vector<?> result : resultList)
System.out.println(result);
}
}
Console log without aspect:
[foo, bar]
[one, two]
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Vector
at de.scrum_master.app.Application.main(Application.java:15)
Uh-oh! This is exactly your problem, right? Now what can we do about it if we absolutely refuse to refactor? We abuse AOP for patching up the legacy code. (Please don't do it, but you can if you absolutely want to.)
AspectJ query result adapter:
Disregarding usage of raw types and other ugly stuff, here is my proof of concept:
package de.scrum_master.aspect;
import java.util.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
#Aspect
public class QueryResultAdapter {
#Around("call(* javax.persistence.Query.getResultList())")
public List<Vector> transformQueryResult(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println(thisJoinPoint);
List result = (List) thisJoinPoint.proceed();
if (result != null && result.size() > 0 && result.get(0) instanceof Vector)
return result;
System.out.println("Transforming arrays to vectors");
List<Vector> transformedResult = new ArrayList<Vector>();
for (Object[] arrayItem : (List<Object[]>) result)
transformedResult.add(new Vector(Arrays.asList(arrayItem)));
return transformedResult;
}
}
Console log with aspect:
call(List de.scrum_master.persistence.VectorQuery.getResultList())
[foo, bar]
[one, two]
call(List de.scrum_master.persistence.ArrayQuery.getResultList())
Transforming arrays to vectors
[foo, bar]
[one, two]
Et voilĂ  - you can do ugly stuff and other things it was not invented for with AOP. ;-)

How to Create Lookup by Account Type using DimensionDynamicAccountController?

I have a problem.
I have in my new table two new fields
1) Name -> AccountNum, EDT--> DimensionDynamicAccount
2) Name -> AccountType, EDT--> LedgerJournalACType
class declaration
:
public class FormRun extends ObjectRun
{
DimensionDynamicAccountController dimAccountController;
}
init (for the form):
public void init()
{
super();
dimAccountController = DimensionDynamicAccountController::construct(
MyTable_ds,
fieldstr(MyTable, LedgerDimension),
fieldstr(MyTable, AccountType));
}
4. Override the following methods on the Segmented Entry control instance in the form design.
public void jumpRef()
{
dimAccountController.jumpRef();
}
public void loadAutoCompleteData(LoadAutoCompleteDataEventArgs _e)
{
super(_e);
dimAccountController.loadAutoCompleteData(_e);
}
public void segmentValueChanged(SegmentValueChangedEventArgs _e)
{
super(_e);
dimAccountController.segmentValueChanged(_e);
}
public void loadSegments()
{
super();
dimAccountController.parmControl(this);
dimAccountController.loadSegments();
}
public boolean validate()
{
boolean isValid;
isValid = super();
isValid = dimAccountController.validate() && isValid;
return isValid;
}
5. Override the following methods on the data source field that backs the Segmented Entry control.
public Common resolveReference(FormReferenceControl _formReferenceControl)
{
return dimAccountController.resolveReference();
}
Now my problem is Lookup only works for AccountType=="Ledger" not for customer, Vendor etc...
If I have a AccountType == Vendor or similant but different to Ledger I see this
I would want to have same the same thing that's in the LedgerJournalTrans Form
There is a solution,
thanks all,
enjoy
This might be too obvious, but I think you're missing the lookup() method.
See:
\Forms\LedgerJournalTransDaily\Designs\Design\[Tab:Tab]\[TabPage:OverViewTab]\[Grid:overviewGrid]\SegmentedEntry:LedgerJournalTrans_AccountNum\Methods\lookup
public void lookup()
{
if (!ledgerJournalEngine.accountNumLookup(ledgerJournalTrans_AccountNum,
ledgerJournalTrans,
ledgerJournalTrans.OffsetAccountType,
ledgerJournalTrans.parmOffsetAccount(),
ledgerJournalTrans_Asset))
{
super();
}
}

Is Realm's .where faster than filtering a list manually?

I am creating a project using a realm database.
I also have a lot of lists which I wish to filter while the user types.
The biggest one of them will probably be the list of customers
Currently I am using realm by using a wrapper class for each object (if anyone has a better suggestion I'm definitely open to it)
The objects I am using are like this:
public class Customer_Realm extends RealmObject{
private int _id;
private String _name; //...etc
public int get_id(){ return _id; }
public void set_id(int id) { _id = id;}
//etc
}
Public class Customer() {
Customer_Realm _base;
public Customer(Customer_Realm base) { _base = base;}
public int get_id(){ return _base.get_id(); }
public void set_id(int id) { _base.set_id(id);}
//...etc
}
Along with this I have an ArrayListAdapter for customer objects. My question is, would this:
public class CustomerAdapter extends ArrayListAdapter<Customer> {
List<Customer> _customers;
String _filter = null;
public void set_filter(string name) {
_filter = name;
notifyDataSetChanged();
}
#override
public int getCount(){
return get_visible().count();
}
public Customer getItem(int position){
return get_visible().get(position);
}
public List<Customer> get_visible() {
if (_filter == null)
return _customers;
List<Customer> visible = new ArrayList<Customer> ();
foreach (Customer c : _customers){
if (c.get_name().contains(_filter)
visible.add(c);
}
return visible;
}
}
Or would this be better?
public class CustomerAdapter extends ArrayListAdapter<Customer> {
Realm _database;
String _filter = null;
public void set_filter(string name) {
_filter = name;
notifyDataSetChanged();
}
#override
public int getCount(){
return get_visible().count();
}
public Customer getItem(int position){
return get_visible().get(position);
}
public List<Customer> get_visible() {
RealmResults<Customer_Realm> result = null;
if (_filter == null)
result = _database.allObjects(Customer_Realm.class);
else
result = _database.where(Customer_realm.class).contains("_name",_filter);
List<Customer> visible = new ArrayList<Customer> ();
foreach (Customer_Realm c : result){
visible.add(new Customer(c));
}
return visible;
}
}
Unfortunately my database contains only a small sample of data(and realm for windows does not have an editor so I can enter data manually) so when I try either implementation I do not see any difference, I was wondering if anyone has any experience with what happens when you use such an implementation with a large number of data
Thanks in advance for any help you can provide

JPA Criteria API group_concat usage

I am currently working on a report which needs a group_concat for one of the fields.
CriteriaQuery<GameDetailsDto> criteriaQuery = criteriaBuilder
.createQuery(GameDetailsDto.class);
Root<BetDetails> betDetails = criteriaQuery.from(BetDetails.class);
Expression<String> betSelection = betDetails.get("winningOutcome");
criteriaQuery.multiselect(
// other fields to select
criteriaBuilder.function("group_concat", String.class, betSelection),
// other fields to select
);
//predicate, where clause and other filters
TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
this throws a null pointer exception on the line:
TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
did i incorrectly use the function method of the criteriaBuilder?
the documentations says:
function(String name, Class<T> type, Expression<?>... args);
I figured out how to do this with Hibernate-jpa-mysql:
1.) created a GroupConcatFunction class extending org.hibernate.dialect.function.SQLFunction (this is for single column group_concat for now)
public class GroupConcatFunction implements SQLFunction {
#Override
public boolean hasArguments() {
return true;
}
#Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
#Override
public Type getReturnType(Type firstArgumentType, Mapping mapping)
throws QueryException {
return StandardBasicTypes.STRING;
}
#Override
public String render(Type firstArgumentType, List arguments,
SessionFactoryImplementor factory) throws QueryException {
if (arguments.size() != 1) {
throw new QueryException(new IllegalArgumentException(
"group_concat shoudl have one arg"));
}
return "group_concat(" + arguments.get(0) + ")";
}
}
2.) i created the CustomMySql5Dialect class extending org.hibernate.dialect.MySQL5Dialect and registered the group_concat class created in step 1
3.) On the app context, i updated the jpaVendorAdapter to use the CustomMySql5Dialect as the databasePlatform
4.) Finally to use it
criteriaBuilder.function("group_concat", String.class,
sampleRoot.get("sampleColumnName"))
Simple solution: instead of creating the whole class, just use SQLFunctionTemplate.
new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)")
and then register this function in your own SQL dialect (eg. in constructor)
public class MyOwnSQLDialect extends MySQL5Dialect {
public MyOwnSQLDialect() {
super();
this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
}
}
Suggested property:
spring.jpa.properties.hibernate.metadata_builder_contributor = com.inn.core.generic.utils.SqlFunctionsMetadataBuilderContributor
and class:
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.spi.MetadataBuilderContributor;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Component;
#Component
public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {
#Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction("config_json_extract",
new StandardSQLFunction("json_extract", StandardBasicTypes.STRING));
metadataBuilder.applySqlFunction("JSON_UNQUOTE",
new StandardSQLFunction("JSON_UNQUOTE", StandardBasicTypes.STRING));
metadataBuilder.applySqlFunction("group_concat",
new StandardSQLFunction("group_concat", StandardBasicTypes.STRING));
}
}

JPA behaviour

I have some trouble understanding a JPA behaviour. Mabye someone could give me a hint.
Situation:
Product entity:
#Entity
public class Product implements Serializable {
...
#OneToMany(mappedBy="product", fetch=FetchType.EAGER)
private List<ProductResource> productResources = new ArrayList<ProductResource>();
....
public List<ProductResource> getProductResources() {
return productResources;
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (!(obj instanceof Product)) return false;
Product p = (Product) obj;
return p.productId == productId;
}
}
Resource entity:
#Entity
public class Resource implements Serializable {
...
#OneToMany(mappedBy="resource", fetch=FetchType.EAGER)
private List<ProductResource> productResources = new ArrayList<ProductResource>();
...
public void setProductResource(List<ProductResource> productResource) {
this.productResources = productResource;
}
public List<ProductResource> getProductResources() {
return productResources;
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (!(obj instanceof Resource)) return false;
Resource r = (Resource) obj;
return (long)resourceId==(long)r.resourceId;
}
}
ProductResource Entity:
This is a JoinTable (association class) with additional properties (amount). It maps Product and Resources.
#Entity
public class ProductResource implements Serializable {
...
#JoinColumn(nullable=false, updatable=false)
#ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
private Product product;
#JoinColumn(nullable=false, updatable=false)
#ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
private Resource resource;
private int amount;
public void setProduct(Product product) {
this.product = product;
if(!product.getProductResources().contains((this))){
product.getProductResources().add(this);
}
}
public Product getProduct() {
return product;
}
public void setResource(Resource resource) {
this.resource = resource;
if(!resource.getProductResources().contains((this))){
resource.getProductResources().add(this);
}
}
public Resource getResource() {
return resource;
}
...
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (!(obj instanceof ProductResource)) return false;
ProductResource pr = (ProductResource) obj;
return (long)pr.productResourceId == (long)productResourceId;
}
}
This is the Session Bean (running on glassfish).
#Stateless(mappedName="PersistenceManager")
public class PersistenceManagerBean implements PersistenceManager {
#PersistenceContext(unitName = "local_mysql")
private EntityManager em;
public Object create(Object entity) {
em.persist(entity);
return entity;
}
public void delete(Object entity) {
em.remove(em.merge(entity));
}
public Object retrieve(Class entityClass, Long id) {
Object entity = em.find(entityClass, id);
return entity;
}
public void update(Object entity) {
em.merge(entity);
}
}
I call the session Bean from a java client:
public class Start {
public static void main(String[] args) throws NamingException {
PersistenceManager pm = (PersistenceManager) new InitialContext().lookup("java:global/BackITServer/PersistenceManagerBean");
ProductResource pr = new ProductResource();
Product p = new Product();
Resource r = new Resource();
pr.setProduct(p);
pr.setResource(r);
ProductResource pr_stored = (ProductResource) pm.create(pr);
pm.delete(pr_stored);
Product p_ret = (Product) pm.retrieve(Product.class, pr_stored.getProduct().getProductId());
// prints out true ????????????????????????????????????
System.out.println(p_ret.getProductResources().contains(pr_stored));
}
}
So here comes my problem. Why is the ProductResource entity still in the List productResources(see code above). The productResource tuple in the db is gone after the deletion and I do newly retrieve the Product entity. If I understood right every method call of the client happens in a new persistence context, but here i obviously get back the non-refreshed product object!?
Any help is appreciated
Thanks
Marcel
When I use the refresh method in the retrieve method it works.
public Object retrieve(Class entityClass, Long id) {
Object entity = em.find(entityClass, id);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
em.refresh(entity);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return entity;
}
So i assume the problem is the caching of eclipselink. If I use the create method on the ProductResource object, the Product and Resource objects are also stored (cascade=CascadeType.Persist). When I load the Product object again it comes out of the cache and therefore is not actual. So I should just also remove the ProductResource object in the lists of the Product and Resource object and do an update. Right? Calling refresh() in this place doesn't really make sense because it bypasses the caching.
Thanks
Marcel