使用Hibernate和MS SQL与intelliJ IDEA



我使用Hibernate/JPA 2.0与SQL Server和IntelliJ IDEA这是一个示例代码我做测试连接:

public class App {
    public static final String SELECT_QUERY = "select u from UsersEntity as u where u.userId = :userId";
    public static void main(String[] args)
    {
        String userId = "1";
        PersistenceProvider persistenceProvider = new HibernatePersistence();
        EntityManagerFactory entityManagerFactory = persistenceProvider.createEntityManagerFactory("newPersistenceUnit", new HashMap());
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        List<UsersEntity> users = entityManager.createQuery(SELECT_QUERY, UsersEntity.class).setParameter("userId", userId).getResultList();
        System.out.println(users);
        entityManager.close();
    }
}

下面是我在控制台中收到的内容:

"C:Program FilesJavajdk1.6.0_29binjava" -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:Program FilesJetBrainsIntelliJ IDEA 12.0.4bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.6.0_29jrelibalt-rt.jar;C:Program FilesJavajdk1.6.0_29jrelibalt-string.jar;C:Program FilesJavajdk1.6.0_29jrelibcharsets.jar;C:Program FilesJavajdk1.6.0_29jrelibdeploy.jar;C:Program FilesJavajdk1.6.0_29jrelibjavaws.jar;C:Program FilesJavajdk1.6.0_29jrelibjce.jar;C:Program FilesJavajdk1.6.0_29jrelibjsse.jar;C:Program FilesJavajdk1.6.0_29jrelibmanagement-agent.jar;C:Program FilesJavajdk1.6.0_29jrelibplugin.jar;C:Program FilesJavajdk1.6.0_29jrelibresources.jar;C:Program FilesJavajdk1.6.0_29jrelibrt.jar;C:Program FilesJavajdk1.6.0_29jrelibextdnsns.jar;C:Program FilesJavajdk1.6.0_29jrelibextlocaledata.jar;C:Program FilesJavajdk1.6.0_29jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.6.0_29jrelibextsunmscapi.jar;C:Program FilesJavajdk1.6.0_29jrelibextsunpkcs11.jar;D:mobitrackoutproductionweb;D:mobitracklibjavax.persistence.jar;D:mobitracklibhibernate-entitymanager-4.2.0.Final.jar;D:mobitracklibjboss-logging-3.1.0.GA.jar;D:mobitracklibhibernate-core-4.2.0.Final.jar;D:mobitracklibantlr-2.7.7.jar;D:mobitracklibjboss-transaction-api_1.1_spec-1.0.0.Final.jar;D:mobitracklibdom4j-1.6.1.jar;D:mobitracklibhibernate-jpa-2.0-api-1.0.1.Final.jar;D:mobitracklibjavassist-3.15.0-GA.jar;D:mobitracklibhibernate-commons-annotations-4.0.1.Final.jar;D:mobitracklibtools.jar;D:mobitracklibsqljdbc4-4.0.2206.100.jar;C:Program FilesJetBrainsIntelliJ IDEA 12.0.4libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.mobitrack.services.App
26 mars 2013 17:44:18 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
26 mars 2013 17:44:18 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.0.Final}
26 mars 2013 17:44:18 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
26 mars 2013 17:44:18 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=MOBITRACKDB]
26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=sa, password=****, autocommit=true, release_mode=auto}

调试时,我发现它停在:

EntityManagerFactory entityManagerFactory = persistenceProvider.createEntityManagerFactory("newPersistenceUnit", new HashMap());

知道"newPersistenceUnit"是我的持久化单元的名称。

编辑:

是的,它可以在IDEA之外工作。当我使用IDEA中的控制台来查询数据时,它可以工作。

我尝试使用下一个代码:

public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
    try {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}
public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}
我收到与上面相同的结果,调试停止在:
sessionFactory = new Configuration().configure().buildSessionFactory();

Edit2:

我使用了EntityManager:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("newPersistenceUnit");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    List<AdminsEntity> result = entityManager.createQuery( "from AdminsEntity",     AdminsEntity.class ).getResultList();
    for ( AdminsEntity event : result ) {
        logged = event;
    }
    entityManager.getTransaction().commit();
    entityManager.close();
    return logged;

停在:

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("newPersistenceUnit");

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="newPersistenceUnit">
    <class>com.mobitrack.entities.GroupsEntity</class>
    <class>com.mobitrack.entities.ZonesEntity</class>
    <class>com.mobitrack.entities.AdminsEntity</class>
    <class>com.mobitrack.entities.AlarmsEntity</class>
    <class>com.mobitrack.entities.LocationsEntity</class>
    <class>com.mobitrack.entities.UsersEntity</class>
    <class>com.mobitrack.entities.UserGroupsEntity</class>
    <class>com.mobitrack.entities.UserZonesEntity</class>
    <class>com.mobitrack.entities.SettingsEntity</class>
    <class>com.mobitrack.entities.MessagesEntity</class>
    <class>com.mobitrack.entities.ProfilesEntity</class>
    <class>com.mobitrack.entities.ProfileZonesEntity</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:sqlserver://localhost:1433;databaseName=MOBITRACKDB"/>
        <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="hibernate.connection.username" value="admin"/>
        <property name="hibernate.connection.password" value="password"/>
        <property name="hibernate.query.factory_class"
                  value="org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory"/>
    </properties>
</persistence-unit>

任何想法?

这个问题可能是由于方法断点减慢了调试器的速度。

请仔细检查您没有设置任何方法断点。在Settings | Debugger中禁用toString()求值和备选集合视图选项也可以提高性能。

相关内容

  • 没有找到相关文章