Hibernate设置

时间:2014-12-16 12:00:03

标签: java oracle hibernate

我正在编写我的第一个hibernate程序,我无法将其连接到我的数据库 详细信息:DB: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

1 个答案:

答案 0 :(得分:0)

删除架构声明:

<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: