Hibernate的注解
注解是什么?
Hibernate的注解是什么? 简单的说,本来放在hbm.xml文件里的映射信息,现在不用配置文件做了,改由注解来完成。
不使用注解时,用hbm.xml文件进行配置
Product.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.how2java.pojo">
<class name="Product" table="product_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
<property name="price" />
</class>
</hibernate-mapping>
使用注解时,直接在实体类上进行配置
- Product.java
@Entity //表示这是一个实体类,用于映射表
@Table(name = "product_") //表示这是一个类,映射到的表名:product_
public class Product {
int id;
String name;
float price;
//属性注解是配置在属性对应的getter方法上的
@Id //Id 表示这是主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //表示自增长方式使用mysql自带的
@Column(name = "id") //表示映射到字段id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "name") //表示映射到字段name
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price")
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
- hibernate.cfg.xml 配置文件改动,让hibernate支持这种注解的方式
<?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>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.characterEncoding">utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- <mapping resource="com/pojo/Product.hbm.xml" /> --> <!--修改前-->
<mapping class="com.pojo.Product" /> <!--修改后-->
</session-factory>
</hibernate-configuration>
多对一注解
注: 分类的getName上并没有加上@Column(name=“name”),也可以达到映射的效果。
因为getName方法默认会被认为是字段映射。 除非加上了@Transient 才表示不进行映射
Category.java
@Entity
@Table(name = "category_")
public class Category {
int id;
String name;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Product.java
@Entity
@Table(name = "product_")
public class Product {
int id;
String name;
float price;
Category category;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price")
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@ManyToOne //表示多对一关系
@JoinColumn(name="cid") //表示关系字段是cid(用作外键)
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
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>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.characterEncoding">utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- <mapping resource="com/pojo/Product.hbm.xml" /> -->
<mapping class="com.pojo.Product" /> <!--注意修改的两项,添加映射-->
<mapping class="com.pojo.Category" />
</session-factory>
</hibernate-configuration>
一对多注解
在上一步的基础上做如下改动
- 为Category再加product集合,并提供getter和setter
Set<Product> products;
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
- 给getProducts方法加上一对多注解
@Entity
@Table(name = "category_")
public class Category {
int id;
String name;
Set<Product> products;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch=FetchType.EAGER) //表示一对多关系,fetch=FetchType.EAGER 表示不进行延迟加载
@JoinColumn(name="cid") //对应的外键
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
多对多注解
-
像上两步那样,为Product,User,Category 加上类和属性注解
-
加上多对一注解ManyToOne
-
加上一对多注解OneToMany
-
Many To Many
User.java
@Entity
@Table(name="user_")
public class User {
int id;
String name;
Set<Product> products;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
name="user_product",
joinColumns=@JoinColumn(name="uid"),
inverseJoinColumns=@JoinColumn(name="pid")
)
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
Product.java
@Entity
@Table(name="product_")
public class Product {
int id;
String name;
float price;
Category category;
Set<User> users;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
name="user_product", //中间表名称
joinColumns=@JoinColumn(name="pid"), //joinColumns写的都是本表在中间表的外键名称
inverseJoinColumns=@JoinColumn(name="uid") // inverseJoinColumns写的是另一个表在中间表的外键名称
)
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@ManyToOne
@JoinColumn(name="cid")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
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>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.characterEncoding">utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.pojo.Product" />
<mapping class="com.pojo.Category" />
<mapping class="com.pojo.User" /> <!---新增加的映射->
</session-factory>
</hibernate-configuration>
关于cid等字段是存在哪个表中的问题?(在多的那一方中)
1.一对一和多对一的@JoinColumn注解的都是在“主控方”,都是本表指向外表的外键名称。
2.一对多的@JoinColumn注解在“被控方”,即一的一方,指的是外表中指向本表的外键名称。
3.多对多中,joinColumns写的都是本表在中间表的外键名称,
inverseJoinColumns写的是另一个表在中间表的外键名称。
Hibernate注解 VS Hibernate的XML
各自的优缺点:
XML配置方式:
优:容易编辑,配置比较集中,方便修改,在大业务量的系统里面,通过xml配置会方便后人理解整个系统的架构,修改之后直接重启应用即可
缺:比较繁琐,配置形态丑陋, 配置文件过多的时候难以管理
注解方式:
优:方便,简洁,配置信息和 Java 代码放在一起,有助于增强程序的内聚性。
缺:分散到各个class文件中,所以不宜维护,修改之后你需要重新打包,发布,重启应用。
喜欢的朋友可以关注我的个人公众号,后台回复java资料可免费领取资源。