【发布时间】:2019-11-20 11:41:57
【问题描述】:
我正在尝试在 Mysql 中使用 Hibernate 保存点数据。但我无法理解我做错了什么?
我正在使用休眠 5.3.6、jts 1.14、mysql-connector-5.1.18。 数据库有id、name和curr_loc三个字段,类型分别为int、varchar和point
我使用的休眠方言是 org.hibernate.dialect.MySQLDialect。
这是实体类
import com.vividsolutions.jts.geom.Point;
@Entity
@Table(name = "location")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "curr_loc")
private Point curr_loc;
//getters and setters
}
这是 SaveLocation 类的 main 方法中的代码,它使用 hibernate 存储数据
public static void main(String args[]){
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Location.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint( new Coordinate( 10, 5 ) );
Location tempLocation = new Location("AMD", point);
session.beginTransaction();
session.save(tempLocation);
session.getTransaction().commit();
}
当我尝试使用 MySQLSpatial56Dialect 时,它给了我这个错误: 无法获得与查询元数据的连接:无法将名称 [org.hibernate.dialect.MySQLSpatial56Dialect] 解析为策略 [org.hibernate.dialect.Dialect] 线程“主”org.hibernate.service.spi.ServiceException 中的异常:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
此外,如果我将方言保留为 MySQLDialect,然后尝试保存数据,则会出现以下错误: 错误:数据截断:无法从您发送到 GEOMETRY 字段的数据中获取几何对象 原因:com.mysql.jdbc.MysqlDataTruncation:数据截断:无法从您发送到 GEOMETRY 字段的数据中获取几何对象
请建议我做错了什么?为什么我不能使用 MySQLSpatial56Dialect。更重要的是,使用休眠将点存储在数据库中的正确方法是什么。没有适当的例子来实现相同的。我是 hibernate 新手,我无法解决这个问题。
【问题讨论】:
标签: java mysql hibernate geometry hibernate-spatial