Idea 2018.3.2 Create Hibernate Initial Project

Posted by martor on Thu, 22 Aug 2019 12:54:36 +0200

Specific steps:

Show me a list of my projects (where the common jar packages are mainly for importing dbutils that connect to databases, and I import them together for convenience)


1. Create a new Project (take creating a web Project as an example)
Check Web Application + Hibernate
Also check "Create default hibernate configuration and main class" (of course, you can also leave it unchecked and create it manually after the project has been created)

2. Click configure to select the following and complete the creation:

3. Configure the database (my name is mysql, you need to create the database before connecting to the database)
Find the Database, click on the "Test Connection" test connection as shown in the figure, click "Apply" and click OK to connect successfully.

4. Configure hibernate.cfg.xml (with comments on specific functions):

<?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>
        <!--Configure database information-->
        <property name="connection.url">jdbc:mysql:///test_hibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>

        <!-- Part Two: Configuration hibernate information -->
        <!--Output Bottom Layer sql Sentence-->
        <property name="hibernate.show_sql">true</property>
        <!--Output underlying statement format-->
        <property name="hibernate.format_sql">true</property>
        <!--hibernate Help create tables, after configuration update Help us update (table updates, no table creation)-->
        <property name="hbm2ddl.auto">update</property>
        <!--Configuration database dialect-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!--Part 3: Put the mapping file in the core configuration file-->
        <mapping class="customer.Customer"/>
    </session-factory>
</hibernate-configuration>

5.hibernate supports the use of annotations directly, adding corresponding annotations to entity classes without creating mapping files. I use annotations here, but also show mapping files (note that if you use annotations, you need to change something)

1.hibernate Core Profile Changes
<mapping resource="customer/customer.hbm.xml"/>
Change to
<mapping class="customer.Customer"/>

customer.hbm.xml file:

<?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>
    <! -- Correspondence between configuration table and entity class
    class tag
    name attribute: entity class full path
    Table attribute: database table name
    -->
    <class name="customer.Customer" table="customer">
        <! -- Configure entity class id and table id correspondence
        hibernate requires an entity class to have a unique value of an attribute
        -->
        <! - ID tag
        Name attribute: id attribute name in entity class
        column attribute: Generate table field names
        -->
        <id name="id" column="id">
            <! - The only key to set up the automatic growth of database - >
            <generator class="native"></generator>
        </id>
        <property name="username" column="username"/>
        <property name="password" column="password"></property>
        <property name="address" column="address"></property>
    </class>
</hibernate-mapping>

Customer entity class configuration:

@Entity
@Table(name = "customer")
public class Customer {
    @Id  // Represents the primary key
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator") // Self-growth
    @Column(name = "id")  // Class attributes correspond to table fields
    private int id;
    private String username;
    private String password;
    private String address;

    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 getAddress() {
        return address;
    }

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

Test results:

I still have a question after I finish here. I hope you will give me an answer.

hibernate automatically generates table content type size is obviously unreasonable, may be what attributes are not set or other issues?

Topics: Hibernate Database Attribute xml