-1

I need help with this code from sql to Hibernate, please I hope you help me with this work...

SELECT x(coordenadas) as latitud, y(coordenadas) as longitud FROM datos;

I'm a new user in this page :-(

I have two tables called datos and dispositivo, I'm using hibernate from Netbeans for get registers of the table called datos, but the problem is that I dont know how convert this sentence. because my table contains a register with a type data "geometry properties functions" called "coordenadas". GeomFromText("Point(18.30 -99)"). I need get the register of this table as Latitud and Longitud in a list. Help me please :(. I sorry for my English, I'm learning.

I had a table with these registers datos,fecha,dirLat,dirLong,Latitud,Longitud,altura and I take the registers with this method...

package Modelo;

import com.conf.hibernate.HibernateUtil;
import com.hibernate.Datos;
import java.util.List;
import org.hibernate.Session;

public class FuncionesHibernate {
    public List<Datos> Tabla_Datos(){
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();
        List<Datos> lista = (List<Datos>)s.createQuery("From Datos").list();
        s.getTransaction().commit();
        return lista;
    }
}

then with this code from a Servlet, I got Latitud and Longitud...

FuncionesHibernate funcionesHibernate = new FuncionesHibernate();
 List<Datos> lista = funcionesHibernate.Tabla_Datos();

for(Datos d : lista){
latitud=d.getLatitud();
longitud=d.getLongitud();
}

The problem is that the tables was updated, and the registers that contained Latitud and Longitud changed tocoordenadas geometry. Well now I need insert registers for this new way in the console for mySql.

insert into datos values(1,1,"2013-02-02","14:00:00",GeomFromText("Point(18.30 -99)"),20);

and now my method called 'FuncionesHibernate ' can't working.

There isn't another way for 'coordenadas' registers get with the new table? What's the new query for Hibernate??

for this razon I need translate this query to query for Hibernate..

SELECT x(coordenadas) as latitud, y(coordenadas) as longitud  FROM datos;
4

2 回答 2

0

函数从 HQL 传输到底层 SQL,因此您可能可以使用几乎相同的查询(只需指定正确的实体名称)

另一种选择是使用本机查询

于 2013-02-05T06:11:42.817 回答
0

我建议使用本机查询:试试下面的代码

transaction = session.beginTransaction();
String sql = "insert into datos values(1,1,"2013-02-02","14:00:00",GeomFromText("Point(18.30 -99)"),20)";
Query query = session.createSQLQuery(sql);
query.executeUpdate();
session.getTransaction().commit();

session.flush();

如果此代码工作正常,则将硬编码值替换为动态值。

编辑:

在您的类路径中添加hibernate-spatial-mysql-*.jar并尝试以下代码。

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;


Coordinate coord = new Coordinate(52.5, 13.3);
Point point = new GeometryFactory().createPoint(coord);
 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
Query query = session.createQuery("FROM datos WHERE WITHIN(:location, polygon) = true", Datos.class);
query.setParameter("location", point);
session.getTransaction().commit();
于 2013-02-05T06:40:29.370 回答