Hibenate映射属性

在默认情况下,被@Entityt修饰的持久化类的所有属性都会被映射到底层数据表。为了指定某个属性所映射的数据列的详细信息,如列名、列字段长度等,可以在实体类中使用@Column修饰该属性。使用该属性可指定如下的属性。


Hibernate同样允许使用@Access注解修饰该属性,用于单独改变Hibernate对该属性的访问策略。该@Access用于覆盖在持久上指定的@access注解。

1.  @Formula注解

该注解的value属性可指定一个SQL表达式,指定该属性的值将根据表达式来计算。@Formula的value属性允许对象属性包含表达式,包括运用sum、average、max、函数求值的结果。甚至可以根据另外一个表达式的查询结果来计算当前记录的属性值。使用@Formula注解时有如下几个注意点。

1)        value=”(sql)”的英文括号不能少。

2)        value=”()”的括号里面SQL表达式,SQL表达式中的列名与表名应该和数据库对应,而不是和持久化对象的属性对应。

3)        如果需要在@Formula的value属性中使用参数,则直接使用where cur.id=currencyID形式,其中currencyID就是参数,当前持久化对象的currenycyID属性将作为参数传入。

例如下面的持久化类。

@Entity
@Table(name="news_inf")
public class News
{
	// 消息类的标识属性
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// 消息标题
	private String title;
	// 消息内容
	private String content;
	// 消息全部内容,由系统根据公式生成,concat拼接字符串
	@Formula("(select concat(nt.title,nt.content)"
		+ "from news_inf nt where nt.id= id)")
	private String fullContent;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// title的setter和getter方法
	public void setTitle(String title)
	{
		this.title = title;
	}
	public String getTitle()
	{
		return this.title;
	}

	// content的setter和getter方法
	public void setContent(String content)
	{
		this.content = content;
	}
	public String getContent()
	{
		return this.content;
	}

	// fullContent的setter和getter方法
	public void setFullContent(String fullContent)
	{
		this.fullContent = fullContent;
	}
	public String getFullContent()
	{
		return this.fullContent;
	}
}

上面的例子中,用注解中的value指定了生成fullContent属性的SQL表达式,该属性在底层数据表中不会有对应的数据列,该属性值将根据SQL表达式计算。看如下的主程序。

public class NewsManager
{
	public static void main(String[] args)
		throws Exception
	{
		// 实例化Configuration,
		Configuration conf = new Configuration()
		// 不带参数的configure()方法默认加载hibernate.cfg.xml文件,
		// 如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
			.configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			.applySettings(conf.getProperties()).build();
		// 以Configuration实例创建SessionFactory实例
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		// 创建Session
		Session sess = sf.openSession();
		// 开始事务
		Transaction tx = sess.beginTransaction();
//		// 创建消息对象
//		News n = new News();
//		// 设置消息标题和消息内容
//		n.setTitle("title");
//		n.setContent("owen,"
//			+ "网站地址http://www.owen.org");
//		// 保存消息
//		sess.save(n);
		News n = (News)sess.get(News.class , 1);
		// 输出fullContent值
		System.out.println(n.getFullContent());
		// 提交事务
		tx.commit();
		// 关闭Session
		sess.close();
		sf.close();
	}
}

运行上面的程序,需要先运行注释掉的代码,这样可以保证数据库中已有供操作的数据。运行的结果可以看到,输出title属性和content属性连缀面成的字符串——这就是根据SQL表达式计算的结果。

2.  @Generated属性

对于指定了@Generated属性的持久化对象,每当Hibernate执行一条insert(当@Generated的value值为INSERT或ALWAYS时)或update(当@Generated的value属性值为ALWAYS时)语句时,Hibernate会立刻执行一条select语句来获得该数据列的值,并将该值赋给持久化对象的该属性。下面是一个@Generated的例子。

@Entity
@Table(name="news_inf")
public class News
{
	// 消息类的标识属性
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// 消息标题
	private String title;
	// 消息内容
	private String content;
	// 指定@Generated的value为ALWAYS,表明该属性的值由数据库生成,
	// Hibernate会在每次执行insert、update时执行select语句来查询获取该属性的值
	@Generated(GenerationTime.ALWAYS)
	private String fullContent;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// title的setter和getter方法
	public void setTitle(String title)
	{
		this.title = title;
	}
	public String getTitle()
	{
		return this.title;
	}

	// content的setter和getter方法
	public void setContent(String content)
	{
		this.content = content;
	}
	public String getContent()
	{
		return this.content;
	}

	// fullContent的setter和getter方法
	public void setFullContent(String fullContent)
	{
		this.fullContent = fullContent;
	}
	public String getFullContent()
	{
		return this.fullContent;
	}
}

上面的代码中指定fullContent属性将由数据库系统自动生成,为了让数据库系统使fullContent属性(对应full_content数据列)自动生成值,本程序需要触发器支持。

drop database hibernate;
create database hibernate;
use hibernate;
create table news_inf
(
 id int auto_increment primary key,
 title varchar(255) not null,
 content varchar(255),
 full_content varchar(255)
);
DELIMITER |
create trigger t_full_content_gen BEFORE INSERT ON news_inf
	FOR EACH ROW BEGIN
		set new.full_content=concat(new.title,new.content);
	END;
|
DELIMITER ;

该示例程序使用如下代码片段来保存一个News对象。

public class NewsManager
{
	public static void main(String[] args)
		throws Exception
	{
		// 实例化Configuration,
		Configuration conf = new Configuration()
		// 不带参数的configure()方法默认加载hibernate.cfg.xml文件,
		// 如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
			.configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			.applySettings(conf.getProperties()).build();
		// 以Configuration实例创建SessionFactory实例
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		// 创建Session
		Session sess = sf.openSession();
		// 开始事务
		Transaction tx = sess.beginTransaction();
		// 创建消息对象
		News n = new News();
		// 设置消息标题和消息内容
		n.setTitle("title");
		n.setContent("owen,"
			+ "网站地址http://www.owen.org");
		// 保存消息
		sess.save(n);
//		News n = (News)sess.get(News.class , 1);
//		// 输出fullContent值
//		System.out.println(n.getFullContent());
		// 提交事务
		tx.commit();
		// 关闭Session
		sess.close();
		sf.close();
	}
}

3.  @Transient修饰不想持久化保存的属性

在默认情况下,持久化类所有属性会自动映射到数据库表的数据列。如果在实际应用中不想持久保存某些属性,则可以考虑使用@Transient修饰。

@Entity
@Table(name="news_inf")
public class News
{
	// 消息类的标识属性
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// 消息标题
	// @Column指定该属性映射的列信息,此处指定了列名、长度
	@Column(name="news_title" , length=50)
	private String title;
	// 消息内容
	@Transient
	private String content;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// title的setter和getter方法
	public void setTitle(String title)
	{
		this.title = title;
	}
	public String getTitle()
	{
		return this.title;
	}

	// content的setter和getter方法
	public void setContent(String content)
	{
		this.content = content;
	}
	public String getContent()
	{
		return this.content;
	}
}

4. @Enumerated修饰枚举的属性

在某些极端的情况下,持久化的属性不是普通Java类型,而是一个枚举,这个时候可以考虑用@Enumerated修饰。如下,程序定义了一个Season枚举。

 public enum Season
{
	春季,夏季,秋季,冬季
}
下面使用@Enumerated来修饰。
 @Entity
@Table(name="news_inf")
public class News
{
	// 消息类的标识属性
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// 消息标题
	// @Column指定该属性映射的列信息,此处指定了列名、长度
	@Column(name="news_title" , length=50)
	private String title;
	// 消息内容
	private String content;
	@Enumerated(EnumType.ORDINAL)
	@Column(name="happen_season")
	private Season happenSeason;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// title的setter和getter方法
	public void setTitle(String title)
	{
		this.title = title;
	}
	public String getTitle()
	{
		return this.title;
	}

	// content的setter和getter方法
	public void setContent(String content)
	{
		this.content = content;
	}
	public String getContent()
	{
		return this.content;
	}

	// happenSeason的setter和getter方法
	public void setHappenSeason(Season happenSeason)
	{
		this.happenSeason = happenSeason;
	}
	public Season getHappenSeason()
	{
		return this.happenSeason;
	}
}

5. @Lob修饰大数据类型的属性

当持久化的属性为byte[]、Byte[]、或java.io.Serializable类型时,@Lob修饰的属性将映射为底层的Blob列;当持久化类的属性为char[]、Character[]或java.lang.String类型时,@Lob修改的属性将映射为底层的Clob属性。下面是一个实体类的代码。

@Entity
@Table(name="person_inf")
public class Person
{
	@Id // 用于修饰标识属性
	// 指定该主键列的主键生成策略
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// @Column指定该属性映射的列信息,此处指定了列名、长度
	@Column(name="person_name" , length=50)
	private String name;
	@Lob
	@Basic(fetch=FetchType.LAZY)
	@Column(nullable=true)
	private byte[] pic;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// name的setter和getter方法
	public void setName(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return this.name;
	}

	// pic的setter和getter方法
	public void setPic(byte[] pic)
	{
		this.pic = pic;
	}
	public byte[] getPic()
	{
		return this.pic;
	}
}

可用下面的程序来执行持久化。

public class PersonManager
{
	public static void main(String[] args)
		throws Exception
	{
		// 实例化Configuration,
		Configuration conf = new Configuration()
			.configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			.applySettings(conf.getProperties()).build();
		// 以Configuration实例创建SessionFactory实例
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		// 创建Session
		Session sess = sf.openSession();
		// 开始事务
		Transaction tx = sess.beginTransaction();
		// 创建Person对象
		Person person = new Person();
		// 为Person对象的属性设置值
		person.setName("com.owen");
		File file = new File("logo.jpg");
		byte[] content = new byte[(int)file.length()];
		new FileInputStream(file).read(content);
		person.setPic(content);
		// 保存Person对象
		sess.save(person);
		// 提交事务
		tx.commit();
		// 关闭Session
		sess.close();
		sf.close();
	}
}

6. @Basic修饰大数据类型的属性

从上面的例子中,Hibernate加载Person对象时并不立即加载它的pic属性,而是只加载一个“虚拟“的代理,等到程序真正需要pic属性时才从底层数据表中加载数据——这就是典型的代理模式。为了使用这个代理模式,我们用@Basic修饰。使用@Basic可以指定如下属性:

1)        fetch:指定是否延迟加载该属性。该属性可接受FetchType.EAGER、FetchType.LAZY两个值之一,其中前者指定立即加载;后者指定使用延迟加载。

2)        optional:指定该属性映射的数据列是否用延迟加载。

上面一个例子的Person的实体可以改为如下:

@Id // 用于修饰标识属性
	// 指定该主键列的主键生成策略
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// @Column指定该属性映射的列信息,此处指定了列名、长度
	@Column(name="person_name" , length=50)
	private String name;
	@Lob
	@Basic(fetch=FetchType.LAZY)
	@Column(nullable=true)
	private byte[] pic;

7. @Temporal修饰日期类型的属性

     使用@Temportal时可指定一个value属性,该属性支持TemportalType.DATE、TemporalType.TIME、TemportalType.TIMESTAMP三个值之一,用于指定该属性映射到数据表的date、time和timestamp类型的数据列。下面是Person实体类代码。

 @Entity
@Table(name="person_inf")
public class Person
{
	@Id // 用于修饰标识属性
	// 指定该主键列的主键生成策略
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	// @Column指定该属性映射的列信息,此处指定了列名、长度
	@Column(name="person_name" , length=50)
	private String name;
	@Temporal(TemporalType.DATE)
	private Date birth;

	// id的setter和getter方法
	public void setId(Integer id)
	{
		this.id = id;
	}
	public Integer getId()
	{
		return this.id;
	}

	// name的setter和getter方法
	public void setName(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return this.name;
	}

	// birth的setter和getter方法
	public void setBirth(Date birth)
	{
		this.birth = birth;
	}
	public Date getBirth()
	{
		return this.birth;
	}

}

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值