0
我使用休眠/ JPA 2.0與SQL Server和IntelliJ IDEA這裏是一個示例代碼,我做了測試連接:使用Hibernate和MS SQL與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 Files\Java\jdk1.6.0_29\bin\java" -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 12.0.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_29\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\alt-string.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunpkcs11.jar;D:\mobitrack\out\production\web;D:\mobitrack\lib\javax.persistence.jar;D:\mobitrack\lib\hibernate-entitymanager-4.2.0.Final.jar;D:\mobitrack\lib\jboss-logging-3.1.0.GA.jar;D:\mobitrack\lib\hibernate-core-4.2.0.Final.jar;D:\mobitrack\lib\antlr-2.7.7.jar;D:\mobitrack\lib\jboss-transaction-api_1.1_spec-1.0.0.Final.jar;D:\mobitrack\lib\dom4j-1.6.1.jar;D:\mobitrack\lib\hibernate-jpa-2.0-api-1.0.1.Final.jar;D:\mobitrack\lib\javassist-3.15.0-GA.jar;D:\mobitrack\lib\hibernate-commons-annotations-4.0.1.Final.jar;D:\mobitrack\lib\tools.jar;D:\mobitrack\lib\sqljdbc4-4.0.2206.100.jar;C:\Program Files\JetBrains\IntelliJ IDEA 12.0.4\lib\idea_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());
明知「newPersi stenceUnit「是我的持久性單元的名稱。
編輯: 是的,它在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>
任何想法?
它在IDEA之外工作嗎? – CrazyCoder 2013-03-29 05:30:58
您可以顯示yor persistenc.xml嗎?你可以嘗試使用「標準」方法創建實體管理器工廠:EntityManagerFactory emf = Persistence.createEntityManagerFactory(「newPersistenceUnit」); – 2013-05-07 18:06:24