时间:2021-07-01 10:21:17 帮助过:31人阅读
在以前弄hibernate的时候,每创建一个数据库表都要在entity下 新建对应的*.hbm.xml 配置文件,当数据库表很多之后就会严重影响开
一、介绍
在以前弄hibernate的时候,每创建一个数据库表都要在entity下 新建对应的*.hbm.xml 配置文件,当数据库表很多之后就会严重影响开发效率和维护成本,所以 就有了注解,原理在于 省去之前繁琐的配置文件,取代的是在实体类中加上以“@”开头的标记,命之为 注解,待会一一解释其中含义:
二、准备
jar 文件:
hibernate-core-3.3.2.GA.jar
ejb3-persistence-1.0.2.GA.jar
hibernate-annotations-3.4.0.GA.jar
hibernate-commons-annotations-3.1.0.GA.jar
javassist-3.11.0.GA.jar
(缺一不可)
三、实现
package com.hibernate.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
* 电影详情类
* @author Administrator
*
*/
@Entity //标记为 数据库表
@Table(name = "tb_film_info") // 指定该实体类映射的对应的数据库表名
public class FilmInfo implements Serializable{
private static final long serialVersionUID = 1L;
private Long fiId;
private String fiName;
private String fiActor;
private String fiDirector;
private Double fiTicketPrice;
private FilmType filmType;
@Id //主键id
@SequenceGenerator(name = "generator", sequenceName = "seq_film_info" ) //指定序列
@GeneratedValue(generator = "generator", strategy = GenerationType.SEQUENCE) //指定主键生成策略
@Column(name = "fi_id") //因为数据表中的结构都是以“_”分割,然后 hibernate默认识别方式没有以任何方式分割,所以每个列都要在getter()方法标记
public Long getFiId() {
return fiId;
}
public void setFiId(Long fiId) {
this.fiId = fiId;
}
@Column(name = "fi_name")
public String getFiName() {
return fiName;
}
public void setFiName(String fiName) {
this.fiName = fiName;
}
@Column(name = "fi_actor")
public String getFiActor() {
return fiActor;
}
public void setFiActor(String fiActor) {
this.fiActor = fiActor;
}
@Column(name = "fi_director")
public String getFiDirector() {
return fiDirector;
}
public void setFiDirector(String fiDirector) {
this.fiDirector = fiDirector;
}
@Column(name = "fi_ticket_price")
public Double getFiTicketPrice() {
return fiTicketPrice;
}
public void setFiTicketPrice(Double fiTicketPrice) {
this.fiTicketPrice = fiTicketPrice;
}
@ManyToOne(fetch = FetchType.LAZY) //多对一关联关系,采用@ManyToOne标记,指定下加载方式
@JoinColumn(name = "fi_film_type_id") //指定下 关联的外键
public FilmType getFilmType() {
return filmType;
}
public void setFilmType(FilmType filmType) {
this.filmType = filmType;
}
}
package com.hibernate.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
* 电影类型类
* @author Administrator
*
*/
@Entity//标记为 数据库表
@Table(name = "tb_film_type")// 指定该实体类映射的对应的数据库表名
public class FilmType implements Serializable{
private static final long serialVersionUID = 1L;
private Long ftId;
private String ftName;
private List
@Id
@SequenceGenerator(name = "generator", sequenceName = "seq_film_type")
@GeneratedValue(generator = "generator", strategy = GenerationType.SEQUENCE)
@Column(name = "ft_id")
public Long getFtId() {
return ftId;
}
public void setFtId(Long ftId) {
this.ftId = ftId;
}
@Column(name = "ft_name")
public String getFtName() {
return ftName;
}
public void setFtName(String ftName) {
this.ftName = ftName;
}
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) //一对多关联关系,指定级联方式
@JoinColumn(name = "fi_film_type_id") // 这里指定 tb_Film_info 表的主键id,和引用tb_film_type表的外键都可以。
public List
return filmInfos;
}
public void setFilmInfos(List
this.filmInfos = filmInfos;
}
}
基本就是这个样子,然后以前使用hibernate映射文件配置获得SessionFactory 的方式是Configuration 的实例,但是使用hibernate注解之后要使用AnnotationConfiguration 的实例,,如下: