类级别注解
1、使用注解的目的:为了简化繁琐的ORM映射文件(*.hbm)的配置
2、JPA与Hibernate的关系
.什么是JPA?(全称Java Persistence API(java持久化的API接口));JPA注解是JAVAEE的标准和规范
.JPA 是标准接口,Hibernate是实现,但是其功能是JPA的超集。
.Hibernate通过hibernate-annotation,hibernate-entitymanager,hibnernate-core三个组件来实现JPA。
.一般在实际开发中,优先考虑使用JPA注解,这样更有利与程序的移植和扩展。
3、Hibernate注解的分类
类级别的注解,属性级别注解,映射关系注解
类级别的注解.@Entity——表示实体类(一个实体类对应数据库中的一张表)
类级别的注解.@Table
类级别的注解.@Embeddable——表示它是嵌入类
类级别的注解.Schema 和 catalog
![](http://img.e-com-net.com/image/info10/f97601c3c1aa44d2b9403ac44f308526.jpg)
5、Hibernate类级别注解之@Entity注解
![](http://img.e-com-net.com/image/info10/5bdb9a52277745a69019868b0b10087e.jpg)
![](http://img.e-com-net.com/image/info10/d440de9d2686454797a9bf1754451cda.jpg)
![](http://img.e-com-net.com/image/info10/54eca1928b074a68ad1f94b8b7bf59b3.jpg)
![](http://img.e-com-net.com/image/info10/6ce19e12b3aa4a0ebf6aaed82e44d334.jpg)
![](http://img.e-com-net.com/image/info10/87524d0cb4084dbba544acae73352fdc.jpg)
6、Hibernate类级别注解之@Table注解
![](http://img.e-com-net.com/image/info10/c9b6b1a4d62f424d853028ce998e1b59.jpg)
![](http://img.e-com-net.com/image/info10/38f77f8cdd06449390412822fafcac97.jpg)
从实现的角度来看,各种数据库系统对Catalog和Schema的支持和实现方式千差万别的。
![](http://img.e-com-net.com/image/info10/3f7b678b0059482e9238e907adf63f0c.jpg)
![](http://img.e-com-net.com/image/info10/051160b964ae41b69ac64c06beaa6849.jpg)
7、Hibernate类级别注解之@Embeddable注解
![](http://img.e-com-net.com/image/info10/653d0b281d134094af00cd69f0c43420.jpg)
![](http://img.e-com-net.com/image/info10/f0bf2289a1af49d191011c813495dd76.jpg)
属性级别注解
1、添加方式
一是写在属性字段上面;二是写在属性的get访问器的上面
2、注解分别有 (加粗的)
@Id、@SequenceGenerator、@GeneratedValue、@Column、@Embedded、@EmbeddedId、@Lob、@Version、@Basic、@Transient
3、Hibernate属性级别注解之@Id注解
![](http://img.e-com-net.com/image/info10/7e65ca12ac3f47809c54fc4b252084bf.jpg)
Ps1:对于复合主键,实体类必须实现serializable接口(声明式接口,不需要实现任何方法)。
Ps2:将 String类型 的属性设置成主键是一定要指定该属性的长度,可以用 Column( length=8) 注解来指定,不然 MySQL 会默认让其长度为255,而 MySQL 主键的长度不允许太长。(即使,Ps1成功实现,但是如果Ps2不通过,即使Junit变为绿条,表也创建不成功)
4、Hibernate属性级别注解之@GeneratedValue注解
![](http://img.e-com-net.com/image/info10/7ea51b0ebb324afe8d59540e716fa7fd.jpg)
![](http://img.e-com-net.com/image/info10/92a17efd3def4ebb8cafa3bd6fdf8b71.jpg)
String类型做主键,@GenericGenerator()
![](http://img.e-com-net.com/image/info10/95fc1c7dc2c94b059dd7e799cb28816f.jpg)
![](http://img.e-com-net.com/image/info10/4035622c412341bf87938bc4f59d66a4.jpg)
5、Hibernate属性级别注解之@Column注解
![](http://img.e-com-net.com/image/info10/7415d89d4a0c425a87cb835e5177dfd6.jpg)
6、Hibernate属性级别注解之@Embedded注解
![](http://img.e-com-net.com/image/info10/a8474852a2a040059a12504719e531a4.jpg)
![](http://img.e-com-net.com/image/info10/a6700020f2af47cbad6878c06f3e2cdc.jpg)
7、Hibernate属性级别注解之@EmbeddedId注解
![](http://img.e-com-net.com/image/info10/636a905f99f04189b03d631b2daf49fd.jpg)
![](http://img.e-com-net.com/image/info10/86fe13ebf9a447fc8b0ddc86a695e8f6.jpg)
![](http://img.e-com-net.com/image/info10/8aae4653cac844db90f3548466bfc4d6.jpg)
8、Hibernate属性级别注解之@Transient注解
![](http://img.e-com-net.com/image/info10/92c766066c2448c68233668ac9e73a9d.jpg)
Hibernate关联映射注解
1、Hibernate关联映射注解之实体之间的关系
![](http://img.e-com-net.com/image/info10/f5d8f2b48139420f942f0e3f74adcb16.jpg)
2、Hibernate关联映射注解之 一对一 单向 外键关联
![](http://img.e-com-net.com/image/info10/6c734a5fb0a848aead6d0ac4c727ee24.jpg)
![](http://img.e-com-net.com/image/info10/c7f0790cd02d432aa40bfb780891c9a5.jpg)
![](http://img.e-com-net.com/image/info10/3f4291de59e14281b66a5af1cc5e6127.jpg)
3、Hibernate关联映射注解之 一对一双向 外键关联
![](http://img.e-com-net.com/image/info10/728718c6158a48f5b0883c77d568ec93.jpg)
![](http://img.e-com-net.com/image/info10/a7f5c25bc0494127b613918e910c415f.jpg)
4、Hibernate关联映射注解之一对一单向外键联合主键
![](http://img.e-com-net.com/image/info10/664258769af84ae1bec4542b1ad49b5f.jpg)
5、Hibernate关联映射注解之多对一单向外键关联
![](http://img.e-com-net.com/image/info10/af6aa2da8af8475d9159e67bf63f3e59.jpg)
![](http://img.e-com-net.com/image/info10/93d60ef83bc64e0bbef38f087baf9d78.jpg)
![](http://img.e-com-net.com/image/info10/c645252c2eeb46c29d178f6af9c188ec.jpg)
![](http://img.e-com-net.com/image/info10/11fd93a9c8974463b8f08a530d966057.jpg)
6、Hibernate关联映射注解之一对多单向外键关联
![](http://img.e-com-net.com/image/info10/2ce47d57bb6a4204b3850745d9b9efee.jpg)
![](http://img.e-com-net.com/image/info10/99eb8b1f70954ceead8255e1b43aaa17.jpg)
![](http://img.e-com-net.com/image/info10/f82ccf456ec146c58511822661d1c2d7.jpg)
7、Hibernate关联映射注解之一对多双向外键关联
![](http://img.e-com-net.com/image/info10/932e48f88d814fd28fb8d1a5f575478e.jpg)
![](http://img.e-com-net.com/image/info10/052914f144674fbb8d6a9ebbf23c4844.jpg)
8、Hibernate关联映射注解之 多对多 单向外键关联
![](http://img.e-com-net.com/image/info10/11bd81990b6f44fda78376c89a3b1b17.jpg)
![](http://img.e-com-net.com/image/info10/b90d8b6c513e45059e2eb0a3de9da1e8.jpg)
![](http://img.e-com-net.com/image/info10/0cc27a834afc4291836252279119cd8c.jpg)
![](http://img.e-com-net.com/image/info10/d906771ed8e34985b768b92498422204.jpg)
9、Hibernate关联映射注解之 多对多双向 外键关联
![](http://img.e-com-net.com/image/info10/d04597f154314ecdbb77768b39428153.jpg)
![](http://img.e-com-net.com/image/info10/053525396f4540aca3e1d1b877e0ec33.jpg)