gpt4 book ai didi

java - hibernate 设置

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:56 25 4
gpt4 key购买 nike

我正在编写我的第一个 hibernate 程序,我无法将其连接到我的数据库
详细信息:数据库:Oracle 11g 版本 2
Hibernate:4.3.7。
您可以验证一下我的配置文件吗:

hibernate.cfg.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hibernate.org/xsd/
hibernate-configuration hibernate-configuration- 4.0.xsd"
xmlns="http://www.hibernate.org/xsd/hibernate-configuration">

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">sys</property>
<property name="connection.password">Admin123</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<!-- Names the annotated entity class -->
<mapping class="com.hp.dto.UserDetails"/>

<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Java 类:

   import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

@Id
private int userId;
private String username;

public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

经理类:

  public class HibernateTest
{
public static void main(String[] args){

UserDetails user=new UserDetails();
user.setUserId(1);
user.setUsername("First User");

SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session= sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();

}
}

异常:

 INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Dec 16, 2014 5:13:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at com.hp.hibernate.HibernateTest.main(HibernateTest.java:18)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:228)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
... 3 more

最佳答案

删除架构声明:

<hibernate-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns="http://www.hibernate.org/xsd/hibernate-configuration">

原来如此:

<hibernate-configuration>

如果您想要架构验证,请使用 DTD:

关于java - hibernate 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27504186/

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