属性“生成”不允许出现在元素“属性”中

时间:2019-12-27 09:12:50

标签: hibernate hibernate-mapping

我正在使用XML创建一个休眠程序,但是在运行它时出现错误

  

属性generated不允许出现在元素property中。

但是我为什么没有在任何地方使用generated

下面是我正在使用的文件

Employee.java

package first;

public class Employee {  
   private int id;  
   private String firstName,lastName;  

   public int getId() {  
      return id;  
   }  
   public void setId(int id) {  
      this.id = id;  
   }  
   public String getFirstName() {  
      return firstName;  
   }  
   public void setFirstName(String firstName) {  
      this.firstName = firstName;  
   }  
   public String getLastName() {  
     return lastName;  
   }  
   public void setLastName(String lastName) {  
     this.lastName = lastName;  
   }  
}

employee.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>  
   <!DOCTYPE hibernate-mapping PUBLIC  
     "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
     "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
<hibernate-mapping>  
   <class name="first.Employee" table="employee1">  
      <id name="id">  
         <generator class="assigned"></generator>  
      </id>  

      <property name="firstName"></property>  
      <property name="lastName"></property>  
  </class>  
</hibernate-mapping>  

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
     "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
     "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
<hibernate-configuration>  
   <session-factory>  
      <property name="hbm2ddl.auto">update</property>  
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
      <property name="connection.url">jdbc:mysql://localhost:3306/hib</property>  
      <property name="connection.username">root</property>  
      <property name="connection.password">123456</property>  
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

      <mapping resource="employee.hbm.xml"/>  
  </session-factory>  
</hibernate-configuration>  

StoreData.java

package first;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class StoreData {  

   public static void main( String[] args )  
   {  
       StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
       Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  

       SessionFactory factory = meta.getSessionFactoryBuilder().build();  
       Session session = factory.openSession();  
       Transaction t = session.beginTransaction();  

       Employee e1=new Employee();    
       e1.setId(1);    
       e1.setFirstName("Gaurav");    
       e1.setLastName("Chawla");    

       session.save(e1);  
       t.commit();  
       System.out.println("successfully saved");    
       factory.close();  
       session.close();     
   }
}

错误

    Dec 27, 2019 2:31:28 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {5.3.1.Final}
    Dec 27, 2019 2:31:28 PM org.hibernate.cfg.Environment <clinit>
    INFO: HHH000206: hibernate.properties not found
    Dec 27, 2019 2:31:28 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
    WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
    Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.xml. Message: cvc-complex-type.3.2.2: Attribute 'generated' is not allowed to appear in element 'property'.
        at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
        at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
        at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
        at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165)
        at first.StoreData.main(StoreData.java:15)
    Caused by: javax.xml.bind.UnmarshalException
     - with linked exception:
    [org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: Attribute 'generated' is not allowed to appear in element 'property'.]
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:468)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420)
        at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
        ... 4 more
    Caused by: org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: Attribute 'generated' is not allowed to appear in element 'property'.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
        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:396)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:453)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3231)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2708)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2051)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:741)
        at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:568)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:86)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:246)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:115)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:445)
        ... 6 more

1 个答案:

答案 0 :(得分:1)

您应该更正hibernate.cfg.xml的标题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   ...
</hibernate-configuration>

employee.hbm.xml的标题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   ...
</hibernate-mapping>

您也可以thisthis作为样本。