2

当我想要读取对象时遇到问题,其中(双变量)这是我的代码:

BranchBuilding 表:

@Entity
@Table(name = "branchbuilding", uniqueConstraints={@UniqueConstraint(columnNames={"buildingname","branch_fk"})})//uniqueConstraints={@UniqueConstraint(columnNames={"username","buildingname"})}
public class BranchBuildingEntity implements Serializable {

    @Id
    @GeneratedValue
    private int id;
    @Column(name = "buildingname", length = 64)
    private String buildingName;
    @Column(name = "description", columnDefinition = "mediumtext", length = 16777215)
    private String description;
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "bremainAdr", column = @Column(name = "bremainadr", columnDefinition = "mediumtext", length = 16777215, nullable=true)),
        @AttributeOverride(name = "bmainStreet", column = @Column(name = "bmainstreet", length = 64, nullable=true)),
        @AttributeOverride(name = "bstate", column = @Column(length = 64, nullable=true)),
        @AttributeOverride(name = "bcity", column = @Column(length = 64, nullable=true)),
        @AttributeOverride(name = "bcentralBuilding", column = @Column(name = "bcentralbuilding", columnDefinition = "tinyint", nullable=true)),
        @AttributeOverride(name = "blongitude", column = @Column(nullable=true)),
        @AttributeOverride(name = "blatitude", column = @Column(nullable=true))
    })
    Address buildingAdr;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "branch_fk", referencedColumnName = "id", nullable = false)
    private BranchEntity branch;

    @OneToMany(mappedBy = "branchbuilding", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OnDelete(action=OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private Set<BuildingTelEntity> buildingtel = new HashSet<BuildingTelEntity>();
//some setter and getter

可嵌入地址:

@Embeddable
public class Address implements Serializable {

    String bstate;
    String bcity;
    String bmainStreet;   
    String bremainAdr;
    double blongitude;
    double blatitude;
    int bcentralBuilding;
//Some getter and Setter

这是我的查询管理器:

public List<T> executeQuery(String query, Object... values) throws DatabaseException {
        logger.info("it's at the first of executeQuery(String query, Object... values)");
        List<Object> ret;
        Session sess = null;
        boolean freed = false;
        List<T> result;
        try {
            sess = HibernateUtil.getSession();
            Transaction tx = sess.beginTransaction();
            Query q = sess.createQuery(query);
            for (int i = 0; i < values.length; i++) {
                if (values[i].getClass().getSimpleName().equals("Long")) {
                    q.setLong(i, (Long) values[i]);
                } else if (values[i].getClass().getSimpleName().equals("String")) {
                    q.setString(i, (String) values[i]);
                } else if (values[i].getClass().getSimpleName().equals("Integer")) {
                    q.setInteger(i, (Integer) values[i]);
                } else if (values[i].getClass().getSimpleName().equals("Double")) {
                    q.setDouble(i, (Double) values[i]);
                } else if (values[i].getClass().getSimpleName().equals("Boolean")) {
                    q.setBoolean(i, (Boolean) values[i]);
                } else {
                    q.setEntity(i, values[i]);
                }
            }
            ret = q.list();
            tx.commit();
            result = makeClass(ret);
            HibernateUtil.freeSession(sess);
            freed = true;
        } catch (HibernateException e) {
            logger.error("Hibernate Exception occurred in executeQuery() Method", e);
            e.printStackTrace();
            if (!freed && sess != null) {
                HibernateUtil.freeSession(sess);
            }
            throw new DatabaseException("Can not execute query.");
        } catch (Exception e) {
            logger.fatal("Exception occurred in executeQuery() Method", e);
            System.out.println(e.getMessage());
            throw new DatabaseException("Can not execute query.");
        }

        return result;
    }

这是一个班级制造者:

 private List<T> makeClass(List<Object> input) {
        List<T> ret = new ArrayList<T>();
        int size = input.size();
        for (int i = 0; i < size; i++) {
            T curr = (T) input.get(i);
            ret.add(curr);
        }
        return ret;
    }

我想通过它的地理坐标加载一个 buildingBranch 实体,但是当我设置 Query 时它返回 null。

 return queryManager.executeQuery("from BranchBuildingEntity b where b.buildingAdr.blongitude = 51.6371154785156 AND b.buildingAdr.blatitude = 35.658412064282"); 

or

 return queryManager.executeQuery("from BranchBuildingEntity b where b.buildingAdr.blongitude = ? AND b.buildingAdr.blatitude = ?", longit, latid);//longit and latid both are double 

但是当我使用下面的查询时,它会返回一个正确的结果(我在我的表中通过 lat = 0 和 long = 0 插入一些分支构建)

return queryManager.executeQuery("from BranchBuildingEntity b where b.buildingAdr.blongitude = 0.0 AND b.buildingAdr.blatitude = 0.0"); 

任何人都知道我的问题在哪里?

4

2 回答 2

3

几乎可以肯定与 Hibernate 无关,而只是精确到双精度。关于这个主题已经有很多关于 SO 的问题,但基本上你正在尝试与 51.6371154785156 进行比较,例如,这个值的内部存储可能不完全等于这个值。如果您想以固定精度存储这些坐标,请考虑提供此功能的十进制数据类型,这也将解决您的选择问题。

示例:什么时候应该使用双精度而不是十进制?

于 2009-07-22T11:37:50.317 回答
3

正如大卫 M 所说,不要直接比较双打。使用不同的表示或进行间接比较:

abs(b.buildingAdr.blongitude - 51.6371154785156) < threshold

阈值指示两个经度必须(在数字上)有多接近才能被视为相同。因此,您选择一个最适合您的阈值(例如 0.000000001)。

于 2009-07-22T11:44:53.347 回答