How to create a data table using hibernate

Introduction: Use connect to the database to create the entity class, quickly learned, but for how to create a data table with hibernate indeed took two days, under the learning process of recording.

1, at the outset how data are not search, later came to understand, when you want to add a positive search, create entity classes with a database is the reverse, create a data table with the entity class is positive, so search on it

2, at the beginning of time, always obsessed with problems IDE and plug-ins, no in-depth study conducted for the problems encountered, resulting in wasted a lot of time (in the end did not get)

3, always obsessed with the tutorial mode of operation, in the end only to find that the wording in the new version is not the same, the pit and pit

4, the longest is a waste of time to generate hbm.xml how to use the hibernate file entity class, see the teacher in the tutorial with quite slipped, because of the computer, ide, plug-in installation fails, so in the end can only be handwritten

1. Create hibernate project: Reference: https://blog.csdn.net/fighting_sxw/article/details/80566295 (Tips: the new version and the old version of the test writing style Note)

2, the four primary files need to create a data table hibernate: entity class, the entity class corresponding mapping file (suffix ending the hbm.xml), hibernate configuration file (ending cfg.xml), and the test class

3, the main content corresponding to the following:

Entity classes:

package com.hibernate.entity;


import java.util.Date;

public class Students {

    private int sid;
    private String sname;
    private String gender;
    private Date birthday;
    private String address;

    public Students(){

    }

    public Students(int sid, String sname, String gender, Date birthday, String address) {
        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.birthday = birthday;
        this.address = address;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Students{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
}

 Entity class file corresponding hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.hibernate.entity.Students" table="Students">
        <id name="sid" type="int">
            <column name="SID"/>
            <generator class="assigned"/>
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME"/>
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER"/>
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY"/>
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS"/>
        </property>

    </class>


</hibernate-mapping>

 The hibernate cfg file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/rushi</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">Sys935269</property>

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <! - automatically generated -> 
    <Property name = "the hbm2ddl.auto"> Create </ Property> 
    <! - Database Dialect -> 
    <Property name = "the hibernate.dialect"> org.hibernate.dialect.MySQL55Dialect </ Property> 

    <-! resource loading -> 
    <Mapping resource = "COM / Hibernate / Entity / Students.hbm.xml" /> 

  </ the session-Factory> 
</ Hibernate-Configuration>

 4, this class focuses on the test, when testing the new version and the old version may be different, may never follow the tutorial are unable to find the problem (Reference: https://blog.csdn.net/bingjianit/ Article This article was / the Details / 68.95425 million )

package com.hibernate.entity;

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;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;

import java.util.Date;

public class Test {

    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init() {

        // create the configuration object 
        // = the configure the Configuration new new the Configuration () the configure ();. 

        // Create a service registry objects 
        // ServiceRegistry ServiceRegistry = new new StandardServiceRegistryBuilder () applySettings (configuration.getProperties ()) Build ();.. 

        // // the above is an older version of the wording, if ignored, can report org.hibernate.MappingException: Unknown entity: com.hibernate.entity.Student this error 

        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder () configure () build ();.. 
        // create a session factory object 
        SessionFactory sessionFactory = new new MetadataSources (ServiceRegistry) .buildMetadata () the buildSessionFactory ();. 
        // session object 
        the session = sessionFactory.openSession (); 
        // open affairs 
        Transaction = session.beginTransaction ();
    } 

    @After 
    public void Destory () { 
        // commit the transaction 
        transaction.commit (); 
        // Close Session 
        Session.close (); 
        // Close session factory 
        sessionFactory.close (); 
    } 

    @ org.junit.Test 
    public void TestSaveStudents () { 
        // generates student objects 
        students s = new students (2, " Zhang San", "South", new Date (), "Wudang"); 
        Session.save (S); 
    } 
}

 Program run ends, may be prompted to fail, but the data already exists in the table, unsure of where you can go to see the next data

 

 

 

 

 

 

 

 

Motto: Never is a process familiar to the familiar, no fear,

 

Guess you like

Origin www.cnblogs.com/lindaiyu/p/10990525.html