Hibernate,xml映射问题

时间:2017-05-18 09:59:02

标签: java xml hibernate jsp

这是我的第一个使用hibernate的应用程序,在这个程序中我有一个jsp文件,同时点击提交按钮调用SaveServlet.java,它从文本框中获取值,但是在运行项目时显示错误,有些请帮助我。

A product ... can be ... new ... NPD
A product ... can be ... existing ... Re-design
A Product ... has ... structure, ... has ... function, ... has ... behavior

持久化类User.java

ERROR:   HHH000196: Error parsing XML (40) : Element type "hibernate-mapping" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "default-cascade" must be declared for element type "hibernate-mapping".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "default-access" must be declared for element type "hibernate-mapping".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "default-lazy" must be declared for element type "hibernate-mapping".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "auto-import" must be declared for element type "hibernate-mapping".
ERROR:   HHH000196: Error parsing XML (40) : Element type "class" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "table" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "mutable" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "polymorphism" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "dynamic-update" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "dynamic-insert" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "select-before-update" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "class".
ERROR:   HHH000196: Error parsing XML (40) : Element type "id" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "id".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "id".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "id".
ERROR:   HHH000196: Error parsing XML (40) : Element type "generator" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "class" must be declared for element type "generator".
ERROR:   HHH000196: Error parsing XML (40) : Element type "property" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "unique" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "lazy" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "generated" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Element type "property" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "unique" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "lazy" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "generated" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Element type "property" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "unique" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "lazy" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "generated" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Element type "property" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "unique" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "lazy" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "generated" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Element type "property" must be declared.
ERROR:   HHH000196: Error parsing XML (40) : Attribute "name" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "column" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "type" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "unique" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "optimistic-lock" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "lazy" must be declared for element type "property".
ERROR:   HHH000196: Error parsing XML (40) : Attribute "generated" must be declared for element type "property".
" Error in SaveServlet  org.hibernate.InvalidMappingException: Unable to read XML"

和此映射文件user.hbm

import com.hiber.Bean
public class User {
     private int id;
    private String username,password,email,phone,city;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

这是配置文件hibernate.cfg.xml

<hibernate-mapping>
    <class name="User" table="user">
        <id name="id" column="id" type="integer">
            <generator class="assigned"></generator>
        </id>
        <property name="username" column="username" type="java.lang.String"></property>
        <property name="password" column="password" type="java.lang.String"></property>
        <property name="email" column="email" type="java.lang.String"></property>
        <property name="phone" column="phone" type="java.lang.String"></property>
        <property name="city" column="city" type="java.lang.String"></property>
    </class>
</hibernate-mapping>

Dao类文件UserDao.java

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/brinkle?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <mapping resource="com/hiber/Bean/User.xml"/>
  </session-factory>
</hibernate-configuration>

HibernateUtil.java

package com.hiber.Dao

public class UserDao {
    Session session=NewHibernateUtil.getSessionFactory().openSession();

        Transaction tran=session.beginTransaction();
         public void addUser(String username,String password,String email,String phone,String city)
         {
        User user=new User();


        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setCity(city);

        session.save(user);
        tran.commit();

        System.out.println("saved succesfully");

}
}

这是我的servlet SaveServlet.java

public class NewHibernateUtil {

    private static  SessionFactory sessionFactory;

      public static SessionFactory getSessionFactory()
   {
       if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);           
        }
       else
       {
           System.out.println("Session not created..........");
       }

        return sessionFactory;

}
}

2 个答案:

答案 0 :(得分:0)

将它放在hbm.xml的标题中:

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

并将其放在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">

答案 1 :(得分:0)

User.hbm.xml

 <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated May 18, 2017 3:31:25 PM by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="com.hiber.Bean.User" table="USERData">
            <id name="id" type="int">
                <column name="ID" />
                <generator class="increment" />
            </id>
            <property name="username" type="java.lang.String">
                <column name="USERNAME" />
            </property>
            <property name="password" type="java.lang.String">
                <column name="PASSWORD" />
            </property>
            <property name="email" type="java.lang.String">
                <column name="EMAIL" />
            </property>
            <property name="phone" type="java.lang.String">
                <column name="PHONE" />
            </property>
            <property name="city" type="java.lang.String">
                <column name="CITY" />
            </property>
        </class>
    </hibernate-mapping>

的hibernate.cfg.xml

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://<host><:port>/<database></property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.default_schema">databasename</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>
            <mapping resource="com/hiber/Bean/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

SaveServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String username=request.getParameter("username");
        String password =request.getParameter("password");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        String city =request.getParameter("city");
        System.out.println("the name is "+username+"pass is  "+password+" emial is "+email +" phone is "+phone +" and city is "+city);
        try
        {
//            System.out.println("here we go");
          UserDao ud=new UserDao();
         ud.get();
          ud.addUser(username, password, email, phone, city);
         response.sendRedirect("welcomeJSF.jsp");
        }
        catch(HibernateException e)
        {
            System.out.println("Error in SaveServlet  "+e);
        }

UserDao.java

package com.hiber.Bean;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UserDao {
    Session session;
    Transaction tran;
    public void get(){
    Configuration c=new Configuration();
    c.configure("hibernate.cfg.xml");
    SessionFactory se=c.buildSessionFactory();
     session=se.openSession();
         tran=session.beginTransaction();

    }
         public void addUser(String username,String password,String email,String phone,String city)
         {
        User user=new User();


        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setCity(city);

        session.save(user);
        tran.commit();

        System.out.println("saved succesfully");

}
         }