Hibernate学习之类级别注解
© 版权声明:本文为博主原创文章,转载请注明出处
类级别注解:
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&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学习之类级别注解的更多相关文章
- Hibernate学习笔记:注解@OneToMany和@ManyToOne的单独使用问题 不成对使用
以某个实际场景为例,现在两张表:用户表User 订单表Order:很显然用户对订单是一对多的关系.二者注解如下 用户表User @Entity @Table(name="users" ...
- 码农小汪-Hibernate学习8-hibernate关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable
近期我也是有点郁闷,究竟是程序中处理关联关系.还是直接使用外键处理关联关系呢?这个的说法不一致!程序中处理这样的关联关系的话.自己去维护这样的约束.这样的非常乐观的一种做法!或者是直接在数据库中处理这 ...
- Hibernate学习之属性级别注解
© 版权声明:本文为博主原创文章,转载请注明出处 属性级别注解 添加方式 1. 写在属性字段上面 2. 写在属性getter方法上面 @Id:必须,定义了映射到数据库表的主键属性,一个实体可以有一个或 ...
- Hibernate注解----类级别注解以及属性注解详解----图片版本
这篇文章是我在慕课网上学习Hibernate注解的时候进行手机以及整理的笔记. 今天把它分享给大家,希望对大家有用.可以进行收藏,然后需要的时候进行对照一下即可.这样能起到一个查阅的作用. 本文主要讲 ...
- Hibernate学习之注解学习
转自:http://blog.sina.com.cn/s/blog_935ebb670101dnre.html 1.类级别注解 @Entity 映射实体类 @Table 映射数句库表 @En ...
- Hibernate学习一:Hibernate注解CascadeType
http://zy19982004.iteye.com/blog/1721846 ———————————————————————————————————————————————————————— Hi ...
- Hibernate学习笔记(二)
2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...
- Hibernate关系映射(注解)
1.类级别注解 @Entity 映射实体类 @Table 映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...
- 01-hibernate注解:类级别注解准备工作
注解简介: 目的:为了简化繁琐的ORM映射文件(.hbm)的配置. JPA与hibernate的关系 JPA:全称 java Persistence API(java持久化API接口) JPA注解是J ...
随机推荐
- .net core 发布iis 错误
点击iis功能,例如 点击log日志,提示xxx路径下的web.config错误 百度之后 安装NET Core Windows Server Hosting ->DotNetCore.2.0. ...
- 用jmeter进行多用户并发压力测试
测试要求如下,多用户同时登陆web应用程序,并进行操作,查看在多用户操作下,程序的performence.恰好,jemter下有个CSV Data Set Config,它用来设定一组参数,以便在向程 ...
- CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
Xcode 升级到 6.0 后,更新 CocoaPods,出现了如下的警告 [!] The `Paopao [Debug]` target overrides the `PODS_ROOT` buil ...
- ArcMAP中如何将16位保存的卫星底图,转变为8位表示
首先说明,这种转换将会去除影像的投影像素的定义,并在转换后变为黑色的部分.16位的存储,一方面也是定义透明非数据像素点表示的方便.但是这种定义直接加大了影像的大小,不便于与CAD等软件进行交换数据.
- matlab坐标轴设置
1. axis([xmin xmax ymin ymax]) 设置当前图形的坐标范围,分别为x轴的最小.最大值,y轴的最小最大值 2. V=axis 返回包含当前坐标范围的一个行向量 3. axis ...
- Ubuntu16.04 -- 后台进程Nohup
nohup用于使程序在用户退出登陆.关闭终端之后仍能继续运行 用法: nohup your_command & #(符号&使程序在后台运行) exit #(退出nohup模式) 启动后 ...
- 完全理解Gson(3):Gson反序列化
完全理解Gson(2):Gson序列化 完全理解Gson(1):简单入门 本文延续前一篇文章,继续介绍简单基本的Gson用法.这篇文章我们将介绍如何将复杂的JSON对象解析为Java对象,其中Java ...
- ckeditor的详细配置(转)
CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...
- Makefile之字符串函数
1.subst字符串替换函数 $(subst <from>,<to>,<text>) 名称:字符串替换函数——subst. 功能:把字串<text>中的 ...
- 新人补钙系列教程之:AS3事件处理--事件流
一个flash应用程序可能会非常复杂,比如,有很多可视实例嵌套在一起,这样的话会形成一个树形结构,这个结构的根是stage,然后一级级到不同的实例,一般来说,要把这个树形结构倒过来看,即stage在顶 ...