本程序运行环境:IDEA。

  实际上我对hiberbate与注解的关系还不是太清晰。据我所知注解都是Java JPA的,那么我的理解是:hibernate就应该只是通过这些JPA标识及hibernate xml配置文件连接数据库并建立对象映射关系。。。也就是说hibernate干的事可能只是自动操作数据库,而注解并不是hibernate中的。貌似看网上一些博客没怎么提hibernate框架与注解的关系...当然,也是我自己看的资料太乱而不全面、系统,所以才没弄清hibernate框架与注解之间的关系。

  编写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可以解除掉验证警告。

  ps:记得将新建的实体类添加到配置文件的mapping标签中。

  首先是一对一单向外键:

  主键在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包:

  参考资料:

      1.https://blog.csdn.net/fengyao1995/article/details/75073151

      2.https://blog.csdn.net/qq_15571649/article/details/54866254

      3.https://www.imooc.com/learn/524

Java - 使用hibernate配置文件 + JPA annotation注解操作数据库的更多相关文章

  1. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  2. JPA Annotation注解

    JPA & Hibernate 注解 先说说JPA和Hibernate的关系 JPA(Java Persistence API),是Java EE 5的标准ORM接口,也是ejb3规范的一部分 ...

  3. Java笔记(第七篇 JDBC操作数据库)

    JDBC是连接数据库和java程序的桥梁,通过JDBC API可以方便地实现对各种主流数据库的操作.学习java语言,必须学习JDBC技术,因为JDBC技术实在java语言中被广泛使用的一种操作数据库 ...

  4. hibernate之使用Annotation注解搭建项目

    之前开发都是使用xml配置来开发项目,开发起来特别繁琐 大家会发现通过注解大大简化了我们开发流程,使我们从繁琐的XML配置中解放出来. 第一步:新建一个javaweb项目.并将hibernate需要的 ...

  5. Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

    Spring  EL 一:在Spring xml 配置文件中运用   Spring EL Spring EL 采用 #{Sp Expression  Language} 即 #{spring表达式} ...

  6. Hibernate的Annotation注解

    当项目变得比较大的时候,如何还使用hbm.xml文件来配置Hibernate实体就会变得比较复杂.这里Hibernate提供了Annotation注解方式,使得Hibernate的映射文件变得很方便管 ...

  7. Hibernate与Jpa的关系

    JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA的总体思想和现有Hibernate.T ...

  8. Hibernate与Jpa的关系,终于弄懂

    我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate,还有EclipseLink(曾经的toplink),OpenJPA等可供选择,所以使用Jpa的一个好处是,可以更换实 ...

  9. [转]Hibernate与Jpa的关系,终于弄懂

    原文地址:http://blog.sina.com.cn/s/blog_5f1619e80100yoxz.html 我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate, ...

随机推荐

  1. 2、介绍在TensorFlow当中使用不同的方式创建张量tensor

    import tensorflow as tf from tensorflow.python.framework import ops ops.reset_default_graph() #开始一个计 ...

  2. 【代码学习】PYTHON 私有化

    一.私有化 xx: 公有变量_x: 单前置下划线,私有化属性或方法,from somemodule import *禁止导入,类对象和子类可以访问__xx:双前置下划线,避免与子类中的属性命名冲突,无 ...

  3. innerAudiocontext的坑

    链接:https://blog.csdn.net/sourcemyx/article/details/79424004 像wx.onNetworkStatusChange(function(){})回 ...

  4. 树莓派raspbian安装matchbox-keyboard虚拟键盘

    环境:raspbian-stretch(2018-06-27) 树莓派:3代B型 官网安装地址:http://ozzmaker.com/virtual-keyboard-for-the-raspber ...

  5. Cookie API介绍

    一.Java提供的操作Cookie的API Java中的javax.servlet.http.Cookie类用于创建一个Cookie Cookie类的主要方法 No. 方法 类型 描述 Cookie( ...

  6. C++标准库里面没有字符分割函数split,自己编写函数实现字符串分割功能

    #include <vector> #include <string> #include <iostream> using namespace std; vecto ...

  7. 计算机二级-C语言-程序设计题-190118记录-通过数组和指针两种方式对字符串进行处理。

    //编写一个函数fun,比较两个字符串的长度,(不使用C语言提供的求字符串长度的函数),函数返回较长的字符串.若两个字符长度相同,则返回第一个字符串. //重难点:通过数组处理和通过指针进行处理的不同 ...

  8. android studio :Timeout waiting to lock daemon addresses registry

    一.开发中 android studio 突然遇到下面的错误提示: Error:Timeout waiting to lock daemon addresses registry. It is cur ...

  9. 03-Docker-Engine详解

    目录 03-Docker-Engine详解 摆脱 LXC 摒弃大而全的 Docker daemon 开放容器计划(OCI)的影响 runc containerd 启动一个新的容器 该模型的显著优势 s ...

  10. acm数论之旅(转载)--素数

    https://www.cnblogs.com/linyujun/p/5198832.html 前言:好多学ACM的人都在问我数论的知识(其实我本人分不清数学和数论有什么区别,反正以后有关数学的知识我 ...