Java - 使用hibernate注解操作数据库

Posted darkchii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java - 使用hibernate注解操作数据库相关的知识,希望对你有一定的参考价值。

  本程序运行环境:IDEA。

  编写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.url">jdbc:mysql://localhost:3306/hibernate?useSSL=false&useUnicode=true&characterEncoding=UTF-8</property>

        <property name="hibernate.connection.username">root</property>

        <property name="hibernate.connection.password">******</property>

        <!-- 数据库的方言:根据底层的数据库生成不同的SQL -->
        <property  name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 配置显示SQL -->
        <property name="hibernate.show_sql">true</property>

        <!-- 配置格式化SQL -->
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="onetoone.IDCard"/>
        <mapping class="onetoone.Students"/>

    </session-factory>

</hibernate-configuration>

  注意到:

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=false&useUnicode=true&characterEncoding=UTF-8</property>

  该标签中结构为:jdbc:mysql://主机地址/数据库名称/取消SSL验证&Unicode编码&utf-8编码

  加上useSSL=false可以解除掉验证警告。

  我的主键在Students类中:

package onetoone;


import javax.persistence.*;

@Entity
public class Students {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private String sex;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "sid", unique = true)
    private IDCard idCard;

    public Students() {

    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public IDCard getIdCard() {
        return idCard;
    }

    public void setIdCard(IDCard idCard) {
        this.idCard = idCard;
    }

    public Students(String sex, IDCard idCard) {
        this.sex = sex;
        this.idCard = idCard;
    }

}

  主键即@ID下对应的类成员变量id,设置为自动增长。

  外键在IDCard中:

package onetoone;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class IDCard {

    @Id
    @GeneratedValue(generator = "sid")
    @GenericGenerator(name = "sid", strategy = "assigned")
    @Column(length = 6)
    private int sid;
    @Column
    private String name;
    @Column
    private String school;
    @Column
    private String address;

    public IDCard() {

    }

    public IDCard(int sid, String name, String school, String address) {
        this.sid = sid;
        this.name = name;
        this.school = school;
        this.address = address;
    }

    public int getSID() {
        return sid;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getAddress() {
        return address;
    }

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

  外键即@ID下的类成员sid,设置为手动设置。

  测试单元:

package main;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

import java.util.EnumSet;

public class Main {
    public static void main(String[] args) {
        ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        Metadata metadata = new MetadataSources(registry).buildMetadata();
        SchemaExport export = new SchemaExport();
        export.create(EnumSet.of(TargetType.DATABASE),metadata);
    }
}

  测试结果使用navicat查看:

  idcard:

技术分享图片

  students:

技术分享图片

  外键:

技术分享图片

   一些需要用到的hibernate核心jar包:

技术分享图片

  参考资料:很多。。。

以上是关于Java - 使用hibernate注解操作数据库的主要内容,如果未能解决你的问题,请参考以下文章

[Hibernate] 注解映射例子

Hibernate @Temporal 注解处理Java与Mysql时间日期类型的映射关系

Hibernate

Java Hibernate的优劣

Hibernate的注解和检索

Spring Boot / Thymeleaf / Hibernate:带有 Java 注解的 Sessionfactory Bean