(1)helloworld:@Table、@Column、@Transient、@Temporal、@Enumerated
来源:互联网 发布:mysql安装教程mac 编辑:程序博客网 时间:2024/06/05 15:57
一、需要配置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> <!-- Database connection settings --> <property name="connection.url">jdbc:mysql://localhost:3306/lianxi</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout sql错误调试用--> <property name="show_sql">true</property><!-- 当执行sql语句是,将sql语句在控制台打印,为true。一般都设为true --> <property name="format_sql">true</property><!-- 会将sql语句格式化显示 ,而不再一行显示信息 create table student ( id integer not null, name varchar(255), age integer, primary key (id) ) --> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property><!-- 当没有这张表时,自动创建 ,create最常用--> <!-- update: 在编程的过程中,需要向表中添加新的字段,不用在数据库中添加新字段,程序运行起来后,会自动添加--> <!-- create-drop :使用 create-drop时,在显式关闭SessionFactory时,将drop掉数据库schema. --> <!-- validate:检查数据库中的表和配置文件中的字段是否一一对应 --> <mapping class="com.model.teacher"/> </session-factory></hibernate-configuration>
二、实体类
@Entity 注解将一个类声明为一个实体bean(即一个持久化POJO类)//从这个包(javax.persistence.Entity)可以看出,是个标准,不依赖于hibernate@Table(name="_Teacher") //当model类和数据库的表名,不一致时,应该在这里指定。在这里类名为:teacher,表名是:_Teacher。若不定义@Table时,系统自动使用默认值:实体的短类名(不包含包名)public class teacher {//实体中非static非transient的属性都可以被持久化,除非注解为@Trainsient。所有没有定义注解的属性相当于其上添加了@Basic注解。 private int id; private String name; private int age; private String sex; private Date birthDate; private ZhiCheng zhicheng; @Id //必须加在getId上面 public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="_name", length=10,nullable=false)//当类中属性名和数据库中字段名不一致时,通过这个注解,就能实现类中字段name到数据库字段_name的映射。但要注意要写在get方法上面 //若对数据库中的属性字段有限制,可以在@Column中设定 public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic //对于没有写注解的属性默认等价于上面添加了@Basic注解 public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Transient //不希望该字段存入数据库中, public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } //new Date() 记录到数据库中,会是timestamp类型,既有日期也有时间。可以只记录时间或者日期。当TemporalType.DATE,就是只记录日期,而不记录时间,对应的数据库属性就变成了date类型 @Temporal(TemporalType.DATE) public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } @Enumerated(EnumType.ORDINAL)//当类型为STRING时,存入数据库的是字符串,字段类型是varchar,当类型为ORDINAL时,存入数据库的是索引值,字段类型是integer public ZhiCheng getZhicheng() { return zhicheng; } public void setZhicheng(ZhiCheng zhicheng) { this.zhicheng = zhicheng; }}public enum ZhiCheng { A,B,C}
一、声明基本的属性映射
①@Basic注解 通过此注解可以声明属性的获取方式(fetch strategy)对于简单的属性,默认的获取方式是即时获取。当一个实体Bean的实例被创建时,hibernate会将这些属性的值从数据库中取出,保存在Bean的属性中,延迟获取(lazy fetch),如果一个属性的获取方式是延迟获取,hibernate在创建一个实体bean的实例时,不会即时将这个属性从数据库中读出,只是在该实体bean的这个属性第一次被调用时,hibernate才去获取对应的值,而不是不能获取该数据。
②@Temporal注解:在核心的JavaAPI中并没有定义时间精度,因此处理时间类型数据时,需要定义将其存储在数据库中所预期的精度。在数据库中,表示时间类型的数据有date、time、timestamp,默认情况下,即不写@Temporal情况下是timestamp.
③@Column注解:在建表时会用到,name(列名)、unique(唯一约束,类似主键特性,但是一个表可以有多个字段是唯一约束,只有一个主键)、nullable、insertable、updateable、length……
④@Enumerated:将枚举类型映射成一个顺序列(保存对应的序列值ORDINAL)或者一个字符串类型的列(保存相应的字符串STRING),默认是保存枚举的序列值,可以通过该注解改变
阅读全文
0 0
- (1)helloworld:@Table、@Column、@Transient、@Temporal、@Enumerated
- Hibernate基础(4):基础配置@Table@Column@Temporal@Transient@Enumerated
- Hibernate基础之四:基础配置@Table@Column@Temporal@Transient@Enumerated
- Hibernate4基础之四:基础配置@Table@Column@Temporal@Transient@Enumerated
- entity类中的transient,column,table,id,entity
- 映射时间(Temporal)类型@Temporal
- 映射时间(Temporal)类型@Temporal
- JPA 映射时间(Temporal)类型@Temporal
- 映射时间(Temporal)类型@Temporal
- JPA 映射时间(Temporal)类型@Temporal
- JPA 映射时间(Temporal)类型@Temporal
- 映射时间(Temporal)类型@Temporal
- JPA 映射时间(Temporal)类型@Temporal
- @Temporal JPA 映射时间(Temporal)类型
- @Temporal JPA 映射时间(Temporal)类型
- 映射时间(Temporal)类型@Temporal
- SQL Server 2016新特性: Temporal table
- 关于HashMap 中的 transient table
- iOS 为UIViewController增加基础性功能的分类
- 1057第四届程序设计大赛 统计数字
- 深度理解“CPU内部寻址方式”
- jquery选取表单元素的方法
- 笨方法学Python习题 45: 对象、类、以及从属关系
- (1)helloworld:@Table、@Column、@Transient、@Temporal、@Enumerated
- SSH-CRUD-删除
- 多态+多态对象模型
- Python数据处理扩展包
- PL/SQ符号大全
- Solidity 字典映射Mappings
- 多线程的理解一
- Java for Web学习笔记(九二):消息和集群(7)RabbitMQ和消息模式(上)
- 数据结构实验之二叉树五:层序遍历