在 Hibernate 中使用注释映射坐标的 Hashmap

发布于 2024-08-28 22:34:49 字数 2082 浏览 4 评论 0原文

我刚刚开始使用 hibernate,我正在尝试将两个坐标之间的步行距离映射到哈希图中,从一个“FromCo整理”到另一个“ToCo整理”可以有很多连接。我不确定我是否正确实现了这一点,我需要什么注释来映射这个 MashMap?感谢

HashMap> coordWalkingConnections = new HashMap>();

@Entity
@Table(name = "COORDCONNECTIONS")
public class CoordinateConnection implements Serializable{

    private static final long serialVersionUID = -1624745319005591573L;

    /** auto increasing id number */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    @id
    private int id;

    @Embedded
    public FromCoordinate fromCoord;

    @Embedded
    public ToCoordinate toCoord;


HashMap<FromCoordinate, ArrayList<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

}

public class FromCoordinate implements ICoordinate
{
    @Column(name = "FROM_LAT")
    private double latitude;

    @Column(name = "FROM_LNG")
    private double longitude;
}

public class ToCoordinate implements ICoordinate
{

    @Column(name = "TO_LAT")
    private double latitude;

    @Column(name = "TO_LNG")
    private double longitude;

    @Column(name = "DISTANCE")
    private double distance;
}

DATABASE STRUCTURE
id  FROM_LAT    FROM_LNG    TO_LAT      TO_LNG      Dist
1   43.352669   -6.264341   43.350012   -6.260653   0.38
2   43.352669   -6.264341   43.352669   -6.264341   0.00
3   46.352669   -6.264341   43.353373   -6.262013   0.17
4   47.352465   -6.265865   43.351290   -6.261200   0.25
5   45.452578   -6.265768   43.352788   -6.264396   0.01
6   45.452578   -6.265768   45.782788   -6.234523   0.01
    .....
    ...
    .

Example HashMap for HashMap<Coordinate, ArrayList<Coordinate>>


<KEY{43.352669  -6.264341}, Arraylist VALUES{(43.350012,-6.260653,0.383657),  (43.352669, -6.264341, 0.000095), (43.353373, -6.262013,  0.173201)}>
<KEY{47.352465  -6.265865}, Arraylist VALUES{(43.351290,-6.261200,0.258781)}>
<KEY{45.452578  -6.265768}, Arraylist VALUES{(43.352788,-6.264396,0.013726),(45.782788,-6.234523,0.017726)}>

I've just started using hibernate and I'm trying to map walking distance between two coordinates into a hashmap, There can be many connections from one "FromCoordinate" to another "ToCoordinate". I'm not sure if i've implemented this correctly, What annotations do i need to map this MashMap? Thanks

HashMap> coordWalkingConnections = new HashMap>();

@Entity
@Table(name = "COORDCONNECTIONS")
public class CoordinateConnection implements Serializable{

    private static final long serialVersionUID = -1624745319005591573L;

    /** auto increasing id number */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    @id
    private int id;

    @Embedded
    public FromCoordinate fromCoord;

    @Embedded
    public ToCoordinate toCoord;


HashMap<FromCoordinate, ArrayList<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

}

public class FromCoordinate implements ICoordinate
{
    @Column(name = "FROM_LAT")
    private double latitude;

    @Column(name = "FROM_LNG")
    private double longitude;
}

public class ToCoordinate implements ICoordinate
{

    @Column(name = "TO_LAT")
    private double latitude;

    @Column(name = "TO_LNG")
    private double longitude;

    @Column(name = "DISTANCE")
    private double distance;
}

DATABASE STRUCTURE
id  FROM_LAT    FROM_LNG    TO_LAT      TO_LNG      Dist
1   43.352669   -6.264341   43.350012   -6.260653   0.38
2   43.352669   -6.264341   43.352669   -6.264341   0.00
3   46.352669   -6.264341   43.353373   -6.262013   0.17
4   47.352465   -6.265865   43.351290   -6.261200   0.25
5   45.452578   -6.265768   43.352788   -6.264396   0.01
6   45.452578   -6.265768   45.782788   -6.234523   0.01
    .....
    ...
    .

Example HashMap for HashMap<Coordinate, ArrayList<Coordinate>>


<KEY{43.352669  -6.264341}, Arraylist VALUES{(43.350012,-6.260653,0.383657),  (43.352669, -6.264341, 0.000095), (43.353373, -6.262013,  0.173201)}>
<KEY{47.352465  -6.265865}, Arraylist VALUES{(43.351290,-6.261200,0.258781)}>
<KEY{45.452578  -6.265768}, Arraylist VALUES{(43.352788,-6.264396,0.013726),(45.782788,-6.234523,0.017726)}>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-09-04 22:34:49

在我看来,你必须在初始化中使用集合的接口类型。

而不是:

HashMap<FromCoordinate, ArrayList<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

使用:

Map<FromCoordinate, List<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

如果您阅读了 hibernate 文档: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html 您将阅读:

实际的接口可能是 java.util.Set、java.util.Collection、
java.util.List、java.util.Map、java.util.SortedSet、
java.util.SortedMap 或任何你喜欢的东西(“任何你喜欢的东西”意味着
你必须编写一个实现
org.hibernate.usertype.UserCollectionType。)

注意实例变量是如何用实例初始化的
哈希集。这是初始化集合值的最佳方式
新实例化(非持久)实例的属性。当你
使实例持久化,例如通过调用 persist() ,
Hibernate 实际上会用一个实例替换 HashSet
Hibernate 自己的 Set 实现。请注意以下事项
错误:

我不知道这是否是唯一的错误。
希望这会对您有所帮助。

Well in my opinion you have to use the interface type of collection in initialization.

Instead of:

HashMap<FromCoordinate, ArrayList<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

use:

Map<FromCoordinate, List<ToCoordinate >> coordWalkingConnections = new HashMap<FromCoordinate, ArrayList<ToCoordinate >>();

If you read the hibernate documentation at: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html you will read:

The actual interface might be java.util.Set, java.util.Collection,
java.util.List, java.util.Map, java.util.SortedSet,
java.util.SortedMap or anything you like ("anything you like" means
you will have to write an implementation of
org.hibernate.usertype.UserCollectionType.)

Notice how the instance variable was initialized with an instance of
HashSet. This is the best way to initialize collection valued
properties of newly instantiated (non-persistent) instances. When you
make the instance persistent, by calling persist() for example,
Hibernate will actually replace the HashSet with an instance of
Hibernate's own implementation of Set. Be aware of the following
errors:

I do not know if this is the only error.
Hope this would help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文