java - 运行 hibernate 应用程序时出现异常

标签 java hibernate

我正在尝试运行 hibernate 应用程序,但发生异常..这是我的代码..任何建议..提前致谢

hibernate.cfg.xml :

<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>

<session-factory>

<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb </property>
<property name="connection.user">root </property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.MYSQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<!-- Related to hibernate properties END-->

<!-- Related to mapping START-->
<mapping resource="user.hbm.xml" />
<!-- Related to the mapping END -->

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

user.hbm.xml :

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>

<class name="DataProvider" table="user_info">

<id name="user_id" column="id">
<genereator class="assigned" />
</id>

<property name="vuser_name" column="name"/>
<property name="user_address" column="address"/>
</class>

</hibernate-mapping>

DataProvider.java :

public class DataProvider {
    private int user_id;
    private String user_name;
    private String user_address;
    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getUser_address() {
        return user_address;
    }
    public void setUser_address(String user_address) {
        this.user_address = user_address;
    }


}

DataInsertion.java :

public class DataInsertion {

    public static void main(String[] args) {
        new DataInsertion().insertInfo();

    }

    public void insertInfo(){
        Configuration con = new Configuration();
        con.configure("hibernate.cfg.xml");
        SessionFactory sf = con.buildSessionFactory();
        Session session = sf.openSession();

        DataProvider provider = new DataProvider();
        provider.setUser_id(121);
        provider.setUser_name("name");
        provider.setUser_address("adress");

        Transaction tr = (Transaction) session.beginTransaction();
        session.save(provider);
        System.out.println("Object Saved");
        try {
            tr.commit();
        } catch (SecurityException | HeuristicMixedException | HeuristicRollbackException | RollbackException
                | SystemException e) {
            e.printStackTrace();
        }
        session.close();
        sf.close();


    }

}

异常(exception)是

Mar 20, 2016 12:49:01 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Mar 20, 2016 12:49:01 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.boot.InvalidMappingException: Could not parse mapping document: user.hbm.xml (RESOURCE) at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46) at org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36) at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59) at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at DataInsertion.insertInfo(DataInsertion.java:22) at DataInsertion.main(DataInsertion.java:15) Caused by: org.hibernate.boot.MappingException: Unable to perform unmarshalling at line number 9 and column 32. Message: cvc-complex-type.2.4.a: Invalid content was found starting with element 'genereator'. One of '{"http://www.hibernate.org/xsd/orm/hbm":meta, "http://www.hibernate.org/xsd/orm/hbm":column, "http://www.hibernate.org/xsd/orm/hbm":type, "http://www.hibernate.org/xsd/orm/hbm":generator}' is expected. : origin(user.hbm.xml) at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:177) at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:61) at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102) at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:57) at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43) ... 10 more Caused by: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 32; cvc-complex-type.2.4.a: Invalid content was found starting with element 'genereator'. One of '{"http://www.hibernate.org/xsd/orm/hbm":meta, "http://www.hibernate.org/xsd/orm/hbm":column, "http://www.hibernate.org/xsd/orm/hbm":type, "http://www.hibernate.org/xsd/orm/hbm":generator}' is expected.] at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171) ... 14 more

最佳答案

您的 user.hbm.xml 中存在拼写错误。你写的是“生成器”而不是“生成器”。修复它,它应该可以工作。

关于java - 运行 hibernate 应用程序时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112742/

相关文章:

java - Google App Engine 退回通知不起作用

java - java.util.Arrays.copyOf(U[], int, Class<? extends T[]>) 中通用通配符的优点

java - 共享重复方法的单独对象

java - Hibernate 二级缓存返回陈旧数据(有时)

java - JPA 延迟加载

java - 我应该在域包中放入哪些类?

java - 考虑到有效字符串的某些限制,在java中分解文本文件的最佳方法是什么?

java - 永不更改表的最佳 Spring/Hibernate 配置

java - 无法摆脱: javax. persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

java - 如何使用 Hibernate 更新实体而不是将其插入数据库?