Hibernate Annotations

1、HibernateAnnotation依赖包

要使用HibernateAnnotation,您可以从Hibernate站点下载Hibernate3.6和HibernateAnnotation库。除了标准的HibernateJAR和依赖项之外,您还需要如下二个库:

  1.HibernateAnnotationjar文件(hibernate-annotations.jar)

  2.Java持久性API(lib/ejb3-persistence.jar)

下一步就是获取Hibernate会话工厂。近期的许多Java项目都使用了轻量级的应用框架,例如Spring。如果您正在使用Spring框架,可以使用AnnotationSessionFactoryBean类轻松建立一个基于注释的Hibernate会话工厂。

2、Hibernate注解语法

我们先稍微讲一下Hibernate的注解语法,详细的语法后面还会完整介绍。

@Entity

声明为一个实体bean

@MappedSuperclass

用在实体的继承过程中的父类上

@Table(name="promotion_info")

为实体bean映射指定表

@Id

声明了该实体bean的标识属性

@GeneratedValue

可以定义标识字段的生成策略

@Transient

将忽略这些字段和属性,不用持久化到数据库

@Column(name="promotion_remark")

声明列,属性还包括(length=200等)

@Temporal(TemporalType.TIMESTAMP)

声明时间格式,支持DATE、TIME、 和 TIMESTAMP三种精度

@Enumerated

声明枚举

@Version

声明添加对乐观锁定的支持

@OneToOne

可以建立实体bean之间的一对一的关联

@OneToMany

可以建立实体bean之间的一对多的关联

@ManyToOne

可以建立实体bean之间的多对一的关联

@ManyToMany

可以建立实体bean之间的多对多的关联

@Formula

一个SQL表达式,这种属性是只读的,不在数据库生成属性(可以使用sum、average、max等)

3、使用示例

如前面提到过的,我们在建立和数据库表对应的实体类时,一般会使用一个基类dto,另一个类继承基类的方式,以达到更加灵活的使用实体类的目的。

UserDto.java:

 1 package com.basis.system.core.user.model;
 2 import javax.persistence.Column;
 3 import javax.persistence.GeneratedValue;
 4 import javax.persistence.Id;
 5 import javax.persistence.MappedSuperclass;
 6 import org.hibernate.annotations.GenericGenerator;
 7 @MappedSuperclass
 8 public class UserDto implements java.io.Serializable{
 9     /**用户ID*/
10     private String userId;
11     /**用户姓名*/
12     private String userName;
13     /**用户代码*/
14     private String userCode;
15     /**用户密码*/
16     private String password;
17     @Id
18     @Column(name="user_id", length=60)
19     @GeneratedValue(generator = "paymentableGenerator")
20     @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
21     public String getUserId() {
22         return userId;
23     }
24     public void setUserId(String userId) {
25         this.userId = userId;
26     }
27     @Column(name="user_name", length=60)
28     public String getUserName() {
29         return userName;
30     }
31     public void setUserName(String userName) {
32         this.userName = userName;
33     }
34     @Column(name="user_code", length=60)
35     public String getUserCode() {
36         return userCode;
37     }
38     public void setUserCode(String userCode) {
39         this.userCode = userCode;
40     }
41     @Column(name="password_", length=60)
42     public String getPassword() {
43         return password;
44     }
45     public void setPassword(String password) {
46         this.password = password;
47     }
48 }

User.java:

1 package com.basis.system.core.user.model;
2 import javax.persistence.Entity;
3 import javax.persistence.Table;
4 @Entity
5 @Table(name="bt_system_user")
6 public class User extends UserDto{
7 }

LogDto.java:

 1 package com.basis.system.core.log.model;
 2 import java.util.Date;
 3 import javax.persistence.Column;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 import javax.persistence.MappedSuperclass;
 7 import org.hibernate.annotations.GenericGenerator;
 8 @MappedSuperclass
 9 public class LogDto implements java.io.Serializable{
10     private String logId;
11     private String info;
12     private Date createTime;
13     @Id
14     @Column(name="log_id", length=60)
15     @GeneratedValue(generator = "paymentableGenerator")
16     @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
17     public String getLogId() {
18         return logId;
19     }
20     public void setLogId(String logId) {
21         this.logId = logId;
22     }
23     @Column(name="info", length=600)
24     public String getInfo() {
25         return info;
26     }
27     public void setInfo(String info) {
28         this.info = info;
29     }
30     @Column(name="create_time")
31     public Date getCreateTime() {
32         return createTime;
33     }
34     public void setCreateTime(Date createTime) {
35         this.createTime = createTime;
36     }
37 }

Log.java:

1 package com.basis.system.core.log.model;
2 import javax.persistence.Entity;
3 import javax.persistence.Table;
4 @Entity
5 @Table(name="bt_system_log")
6 public class Log extends LogDto{
7 }

IUserService.java:

1 package com.basis.system.core.user.service;
2 import com.basis.system.core.user.model.User;
3 public interface IUserService {
4     public void addUser(User user);
5     public void deleteUser(User user);
6     public void updateUser(User user);
7 }

ILogService.java:

1 package com.basis.system.core.log.service;
2 import com.basis.system.core.log.model.Log;
3 public interface ILogService {
4     public void addLog(Log log);
5 }

UserServiceImpl.java:

 1 package com.basis.system.core.user.serviceImpl;
 2 import java.util.Date;
 3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 4 import com.basis.system.core.log.model.Log;
 5 import com.basis.system.core.log.service.ILogService;
 6 import com.basis.system.core.log.serviceImpl.LogServiceImpl;
 7 import com.basis.system.core.user.model.User;
 8 import com.basis.system.core.user.service.IUserService;
 9 public class UserServiceImpl extends HibernateDaoSupport implements
10         IUserService {
11     private ILogService logService;
12     public void setLogService(LogServiceImpl logServiceImpl) {
13         this.logService = logServiceImpl;
14     }
15     public void addUser(User user) {
16         this.getHibernateTemplate().save(user);
17         Log log = new Log();
18         log.setInfo("增加用户");
19         log.setCreateTime(new Date());
20         logService.addLog(log);
21     }
22     public void deleteUser(User user) {
23         this.getHibernateTemplate().delete(user);
24         Log log = new Log();
25         log.setInfo("删除用户");
26         log.setCreateTime(new Date());
27         logService.addLog(log);
28     }
29     public void updateUser(User user) {
30         this.getHibernateTemplate().update(user);
31         Log log = new Log();
32         log.setInfo("更新用户");
33         log.setCreateTime(new Date());
34         logService.addLog(log);
35     }
36 }

LogServiceImpl.java:

1 package com.basis.system.core.log.serviceImpl;
2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
3 import com.basis.system.core.log.model.Log;
4 import com.basis.system.core.log.service.ILogService;
5 public class LogServiceImpl extends HibernateDaoSupport implements ILogService{
6     public void addLog(Log log) {
7         this.getHibernateTemplate().save(log);
8     }
9 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。