(十) 使用Hibernate 注解
- Hibernate里有两种注解 :
- Hibernate 注解
- JPA注解
案例一: 用注解对非复合主键的表生成配置文件
package bean; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity // 表明这个类是一个实体类,所谓实体类就是该类对应数据库中的表
@Table(name = "user") // 该实体类对应着数据库中的user表
public class UserBean { @Id
@GeneratedValue(generator="a")
@GenericGenerator(name="a",strategy="assigned") //设置主键增长机制,assigned为程序中定义,这也是默认方式。
private Integer userid;
private String username;
private String password;
private String sex;
@Column(name = "is_admin") // 表示idAdmin这个属性对应着数据库中的is_admin这个字段
private String isAdmin; public UserBean() { } public UserBean(Integer userid, String username, String password,
String sex, String isAdmin) {
super();
this.userid = userid;
this.username = username;
this.password = password;
this.sex = sex;
this.isAdmin = isAdmin;
} public Integer getUserid() {
return userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getIsAdmin() {
return isAdmin;
} public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
}
}
- 更新总配置文件
<mapping class="bean.UserBean"/>
案例二: 用注解对含有复合主键的表生成配置文件
package bean; import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; /**
* Score entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="score")
public class Score implements java.io.Serializable { // Fields
@Id
@EmbeddedId // 表示主键是一个复合主键
private ScoreId id;
private Integer score; // Constructors /** default constructor */
public Score() {
} /** minimal constructor */
public Score(ScoreId id) {
this.id = id;
} /** full constructor */
public Score(ScoreId id, Integer score) {
this.id = id;
this.score = score;
} // Property accessors public ScoreId getId() {
return this.id;
} public void setId(ScoreId id) {
this.id = id;
} public Integer getScore() {
return this.score;
} public void setScore(Integer score) {
this.score = score;
} }
- ScoreId.java
package bean; import javax.persistence.Embeddable; /**
* ScoreId entity. @author MyEclipse Persistence Tools
*/
@Embeddable //表示该类可被其他类嵌入
public class ScoreId implements java.io.Serializable { // Fields private Integer userid;
private String subject; // Constructors /** default constructor */
public ScoreId() {
} /** full constructor */
public ScoreId(Integer userid, String subject) {
this.userid = userid;
this.subject = subject;
} // Property accessors public Integer getUserid() {
return this.userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getSubject() {
return this.subject;
} public void setSubject(String subject) {
this.subject = subject;
} public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ScoreId))
return false;
ScoreId castOther = (ScoreId) other; return ((this.getUserid() == castOther.getUserid()) || (this
.getUserid() != null && castOther.getUserid() != null && this
.getUserid().equals(castOther.getUserid())))
&& ((this.getSubject() == castOther.getSubject()) || (this
.getSubject() != null && castOther.getSubject() != null && this
.getSubject().equals(castOther.getSubject())));
} public int hashCode() {
int result = 17; result = 37 * result
+ (getUserid() == null ? 0 : this.getUserid().hashCode());
result = 37 * result
+ (getSubject() == null ? 0 : this.getSubject().hashCode());
return result;
} }
- 更新总配置文件
<mapping class="bean.Score"/>
使用注解方式配置bean的时候,Hibernate默认把bean中的所有成员属性都与数据库中的字段相关联,如果有些成员属性并不是数据库中的字段的时候,此时就会报错,
则这时在这些属性前添加 @Transient 注解, 表明这个属性不是数据库的字段。
(十) 使用Hibernate 注解的更多相关文章
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- Hibernate注解----关联映射注解以及课程总结详解----图片版本
上一篇,记录了Hibernate注解----类级别注解以及属性注解详解 ,我们这一节主要讲解的是Hibernate注解----关联映射注解以及课程总结详解. 本节的主要内容: 第3章 关联映射注解 3 ...
- Hibernate注解----类级别注解以及属性注解详解----图片版本
这篇文章是我在慕课网上学习Hibernate注解的时候进行手机以及整理的笔记. 今天把它分享给大家,希望对大家有用.可以进行收藏,然后需要的时候进行对照一下即可.这样能起到一个查阅的作用. 本文主要讲 ...
- hibernate注解随笔—10月8日
hibernate注解(herbinate4 jar包注解可用,使用hibernate3.3注解失败) 如果javabean与数据库中表名一致(不区分大小写),则注解不用写@Table(name=&q ...
- Hibernate注解使用以及Spring整合
Hibernate注解使用以及Spring整合 原文转自:http://wanqiufeng.blog.51cto.com/409430/484739 (1) 简介: 在过去几年里,Hibernate ...
- Hibernate注解映射联合主键的三种主要方式
今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误: org.springframework.beans.factory.BeanCreationException ...
- 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...
- Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:
Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长: 通过Hibernate注解的方式映射oracel数据库的sequence主键生成器 ...
- 。。。Hibernate注解配置的注意事项。。。
今天本来打算录视频的,突然遇到一个拦路虎,Hibernate注解配置,有一个注意点:要么都在属性上面注解配置,要么都在getXX()方法上面用注解配置,要不然就会报错: Caused by: org. ...
随机推荐
- 字节组数(二进制流)、Base64、图片(文件)、二进制相互之间转换
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- The implementation of iterators in C# and its consequences (part 1) Raymond Chen
Likeanonymous methods, iterators in C# are very complex syntactic sugar. You could do it all yoursel ...
- vue 自定义全局方法
import {myfun} from '../static/js/test.js' //se6的正确写法export default {methods:{ diyfun:function () { ...
- ISO/IEC 9899:2011 条款6.4.8——预处理数字
6.4.8 预处理数字 语法 1.pp-number: digit . digit pp-number digit pp-number identifier-nondigit pp- ...
- 010-centos 端口问题
1.nmap 安装 yum install nmap #输入y安装 使用 nmap localhost #查看主机当前开放的端口 nmap -p 1024-65535 local ...
- Git——起步(待续)
原文链接:Getting Started with Git
- npm教程、脚手架
使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资源的 ...
- Kafka Connect Architecture
Kafka Connect's goal of copying data between systems has been tackled by a variety of frameworks, ma ...
- C#将HTML代码转换为图片
前端通过富文本控件接收到了一段html代码,后端想通过图片的形式展示到另外的地方,这种情况怎么处理呢.直接上代码: using System; using System.Collections.Gen ...
- MySQL创建及删除临时表
示例SQL: drop temporary table if exists testdb.tmp_test_table; create temporary table testdb.tmp_test_ ...