Hibernate查询在结果中产生奇怪的符号

时间:2016-08-16 17:57:47

标签: java hibernate

我正在使用带有hibernate的java。

我只是将我的项目设置为使用hibernate,但是当我执行查询时,我得到了奇怪的符号/语义。

我得到以下奇怪的符号/语义,我不确定为什么?

querying all the managed entities... executing: from CarProduct WHERE carid='27883836' Hibernate: select carproduct0_.car_id as car1_0_, carproduct0_.attribute as attribut2_0_, carproduct0_.product_id as product3_0_, carproduct0_.value as value4_0_ from car_product carproduct0_ where carproduct0_.car_id='278838836'

这是我的主要课程:

import java.util.Map;

public class Main {
    private static final SessionFactory ourSessionFactory;
    private static final ServiceRegistry serviceRegistry;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            System.out.println("querying all the managed entities...");
            final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
            for (Object key : metadataMap.keySet()) {
                final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
                final String entityName = classMetadata.getEntityName();
                final Query query = session.createQuery("from " + entityName + " WHERE carid='278838836'");
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
                    System.out.println("  " + o);
                }
            }
        } finally {
            session.close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您看到的是使用SQL aliases的完全正常的SQL查询。

例如因为

from car_product carproduct0_

我们可以在查询的其他部分使用此别名,如下所示:

select carproduct0_.car

这些别名当然不是非常友好的名字。