gpt4 book ai didi

java - 在独立应用程序的 hibernate 状态下配置 sessionFactory

转载 作者:行者123 更新时间:2023-11-29 05:59:52 25 4
gpt4 key购买 nike

我正在尝试制作一个第 3 方简单的独立/swing 应用程序,它使用 hibernate 来连接另一个应用程序的数据库,所以这就是我所做的:

1- 使用的 jar :

hibernate-core-3.5.1-Final
hibernate-entitymanager-3.5.1-Final
hibernate-jpa-2.0-api-1.0.0.Final
hibernate-annotations-3.5.1-Final
hibernate-commons-annotations-3.2.0.Final
dom4j-1.6.1
slf4j-api-1.6.4
slf4j-log4j12-1.6.4
log4j-1.2.16.jar
commons-collections-3.2
jta-1.1
mysql-connector-java-5.1.14 (or compatible connector with your DB)
commons-logging-1.1.1
commons-collections-3.2

2- hibernate.cfg.xml(在 src 文件夹内):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/myapp</property>
<property name="connection.username">myuser</property>
<property name="connection.password">mypass</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>

</session-factory>
</hibernate-configuration>

3- SessionFactoryHelper:

public class SessionFactoryHelper {

private static final SessionFactory sessionFactory;

static {
try {
/*
* Build a SessionFactory object from session-factory configuration
* defined in the hibernate.cfg.xml file. In this file we register
* the JDBC connection information, connection pool, the hibernate
* dialect that we used and the mapping to our hbm.xml file for each
* POJO (Plain Old Java Object).
*/
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable e) {
System.err.println("Error in creating SessionFactory object."
+ e.getMessage());
throw new ExceptionInInitializerError(e);
}
}

/*
* A static method for other application to get SessionFactory object
* initialized in this helper class.
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

4- 示例查询:

Session session = SessionFactoryHelper.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
int count = (Integer) session.createSQLQuery(
"select count(*) from users").uniqueResult();
session.getTransaction().commit();
System.out.println("Users Count: " + count);

运行应用程序时,出现以下异常:

Error in creating SessionFactory object.invalid configuration
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.xeno.xecamp.desktopManagement.SessionFactoryHelper.<clinit>(SessionFactoryHelper.java:24)
at com.myapp.Main.main(Main.java:9)
Caused by: org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1579)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
at com.xeno.xecamp.desktopManagement.SessionFactoryHelper.<clinit>(SessionFactoryHelper.java:19)
... 1 more
Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:250)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1576)
... 4 more

谁能告诉我我的配置有什么问题吗?

最佳答案

问题根本与 Hibernate 无关,而与 XML 结构有关。

SAX 阅读器由 Hibernate 设置为使用验证 (org.hibernate.util.XMLHelper#createSAXReader(String,List,EntityResolver)

它更像这样:

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);

Java 文档说

方法 setValidating(boolean) - 请求 DTD 验证并在不存在 DTD 时导致失败。如果您只需要模式验证而不需要 DTD 验证,请使用 setValidating(false)。

你的错误说得很清楚:

原因:org.xml.sax.SAXParseException:文档无效:未找到语法。

In this tutorial you will find all required information about hibernate conf file.

要修复它,您需要添加:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

关于java - 在独立应用程序的 hibernate 状态下配置 sessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10586512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com