© 版权声明:本文为博主原创文章,转载请注明出处

类级别注解:

  1. @Entity        实体:表示映射实体类,使用@Entity时必须指定实体类的主键属性

                   @Entity(name="")

                   name:可选,对应数据库中的一张表。若表名与实体名相同则可省略

  2. @Table         表:表示实体类对应的数据库表的信息   

                   @Table(name="", catalog="", schema="")

                   name:可选,映射表的名称,默认表名与实体名称一致,只有在不一致的情况下才需指定表名

                   catalog:可选,表示目录名称,默认为Catalog("")

                   schema:可选,表示模式名称,默认为Schema("")

  3. @Embeddable      嵌入类:表示一个非Entity类可以嵌入到一个Entity类中作为属性而存在

实例

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.hibernate</groupId>
<artifactId>Hibernate-ClassAnnotation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.1.7.Final</hibernate.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
</project>

3.Address.java

package org.hibernate.model;

import javax.persistence.Embeddable;

@Embeddable
public class Address { private String postcode;// 邮编
private String address;// 地址
private String telephone;// 电话 public String getPostcode() {
return postcode;
} public void setPostcode(String postcode) {
this.postcode = postcode;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} }

4.Student.java

package org.hibernate.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_student")
public class Student { private long id;// 学号
private String name;// 姓名
private Date birthday;// 出生日志
private String sex;// 性别
private Address address;// 地址 public Student() {
} public Student(long id, String name, Date birthday, String sex, Address address) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.sex = sex;
this.address = address;
} @Id
public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} }

5.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">
<hibernate-configuration> <!-- SessionFactory配置 -->
<session-factory>
<!-- 数据库常用配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">***</property>
<property name="connection.url">
jdbc:mysql:///hibernate?useSSL=true&amp;characterEncoding=UTF-8
</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 开发常用配置 -->
<property name="show_sql">true</property>
        <property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- 引入映射类 -->
<mapping class="org.hibernate.model.Student"/>
</session-factory> </hibernate-configuration>

6.TestClassAnnotation.java

package org.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class TestClassAnnotation { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void before() { sessionFactory = new Configuration().configure().buildSessionFactory();// 创建SessionFactory对象
session = sessionFactory.openSession();// 获取Session对象
transaction = session.beginTransaction();// 开启事务 } @After
public void after() { transaction.commit();// 提交事务
session.close();// 关闭Session
sessionFactory.close();// 关闭SessionFactory } @Test
public void testAnnotation() { System.out.println("执行成功..."); } }

7.效果预览

  7.1 执行testAnnotation()方法-控制台

  7.2 表结构

参考:http://www.imooc.com/video/10073

Hibernate学习之类级别注解的更多相关文章

  1. Hibernate学习笔记:注解@OneToMany和@ManyToOne的单独使用问题 不成对使用

    以某个实际场景为例,现在两张表:用户表User 订单表Order:很显然用户对订单是一对多的关系.二者注解如下 用户表User @Entity @Table(name="users" ...

  2. 码农小汪-Hibernate学习8-hibernate关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable

    近期我也是有点郁闷,究竟是程序中处理关联关系.还是直接使用外键处理关联关系呢?这个的说法不一致!程序中处理这样的关联关系的话.自己去维护这样的约束.这样的非常乐观的一种做法!或者是直接在数据库中处理这 ...

  3. Hibernate学习之属性级别注解

    © 版权声明:本文为博主原创文章,转载请注明出处 属性级别注解 添加方式 1. 写在属性字段上面 2. 写在属性getter方法上面 @Id:必须,定义了映射到数据库表的主键属性,一个实体可以有一个或 ...

  4. Hibernate注解----类级别注解以及属性注解详解----图片版本

    这篇文章是我在慕课网上学习Hibernate注解的时候进行手机以及整理的笔记. 今天把它分享给大家,希望对大家有用.可以进行收藏,然后需要的时候进行对照一下即可.这样能起到一个查阅的作用. 本文主要讲 ...

  5. Hibernate学习之注解学习

    转自:http://blog.sina.com.cn/s/blog_935ebb670101dnre.html 1.类级别注解 @Entity   映射实体类 @Table    映射数句库表 @En ...

  6. Hibernate学习一:Hibernate注解CascadeType

    http://zy19982004.iteye.com/blog/1721846 ———————————————————————————————————————————————————————— Hi ...

  7. Hibernate学习笔记(二)

    2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...

  8. Hibernate关系映射(注解)

    1.类级别注解 @Entity     映射实体类 @Table    映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...

  9. 01-hibernate注解:类级别注解准备工作

    注解简介: 目的:为了简化繁琐的ORM映射文件(.hbm)的配置. JPA与hibernate的关系 JPA:全称 java Persistence API(java持久化API接口) JPA注解是J ...

随机推荐

  1. LCA【bzoj3364】 [Usaco2004 Feb]Distance Queries 距离咨询

    Description  奶牛们拒绝跑马拉松,因为她们悠闲的生活无法承受约翰选择的如此长的赛道.因此约翰决心找一条更合理的赛道,他打算咨询你.此题的地图形式与前两题相同.但读入地图之后,会有K个问题. ...

  2. jcl sort comp3 to 表示型

    Lets say your packed data is at 10th column and is of length 6, S9(4)V99 You could try the following ...

  3. adb 查看android手机中应用的包名和安装位置

    1. 查看是否连接手机 adb devices 2. 进入指定的device的shell adb shell 或者 adb -s ********* shell 3. adb 查看所有安装的包 pm ...

  4. Java重定向IO

    import java.io.*; import java.util.*; public class Main { public static void main(String[] args) thr ...

  5. jsp笔记2(编译指令与动作指令)

    一.jsp的编译指令是通知jsp引擎的消息,不会生成输出. jsp的3个编译指令: page:针对当前页面的指令   include:包含另一个页面的指令   taglib:用于定义和访问自定义标签 ...

  6. Map泛型集合-输入名字输出成绩

    package collection; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import ...

  7. 集合框架(上):学生选课(collection)

    利用集合存储课程信息: 1.Course类 package com.collection; public class Course { public String id; public String ...

  8. 如何快速搜索SQL数据库数据和对象

    原文 如何快速搜索SQL数据库数据和对象 Frequently, developers and DBAs need to search databases for objects or data. I ...

  9. 【转】matlab 字符串处理函数

    原文地址 matlab 字符串处理函数 % 字符串处理 a='  a';b='b  b';c='cccc';m='' % 获取字符串长度 length(a)     % 连接两个字符串,每个字符串最右 ...

  10. Android学Jni/Ndk 开发记录(一)

      治疗拖延症的唯一办法就是:一想起些什么 / 要做些什么就 TM 立马去做! 是的,突然想起我不会 JNI.NDK 开发.解决办法:立马去学! 一:配置 NDK 环境 下载 NDK 写入到配置文件 ...