hibernate常见注解

创建触发器

DELIMITER |
create trigger t_full_content_gen BEFORE INSERT ON news_inf new
 for each row begin
  set new.full_content = concat(new.title,new.content);
  end;
  |
delimiter;

常见注解

  • @Enumerated(EunmType.ORDINAL | EnumType.STRING)
    数据库保存图片、大段文章时通常使用Blob、Clob类型的数据类型保存。
  • Hibernate中使用@Log来修饰这种大数据类型。当持久化类的属性为byte[],Byte[],java.io.Serializable时,@Log映射为底层的Blob类型。当为char[],Character[]或java.lang.String时映射为Clob类型。
  • 指定某个属性是否使用延迟加载。
    @Basic(fetch=FetchType.LAZY | FetchType.EAGER)
  • 指定日期类型;
    @Temporal(TemporalType.DATE | TemporalType.TIME|TemporalType.TIMESTAMP)

映射主键

建议为逻辑主键而非有实际意义的物理主键。

@GeneratedValue属性:

  • strategy:
    1. GenerationType.AUTO:默认值,hibernate自动选择最适合底层数据库的主键生成策略。
    2. GenerationType.IDENTITY:选择自生长的主键生成策略。
    3. GenerationType.SEQUNECE:对于oracle这样的数据库。配合@SequenceGenerator一起使用
    4. GenerationType.TABLE:使用辅助表来生成主键,与@TableGenerator一起使用。
  • generator:当使用GenerationType.SEQUNECE,GenerationType.TABLE时,该属性引用@SequenceGenerator,@TableGenerator所定义的生成器的名字。

注:

JPA标准只提供了以上四个主键生成策略,hibernate还提供了很多主键生成策略,例如hilo,incremnt等,用@GenericGenerator注解来使用。属性:
1. name:生成器的名称
2. strategy:策略

@Id
  @TableGenerator(name="newsGen",table="NewS_ID_GEN",
        pkColumnName="gen_key",valueColumnName="gen_value",
        pkColumnValue="news_id")
  @GeneratedValue(strategy=GenerationType.TABLE)
  private Integer id;
  private String title;
  private String content;
  @Formula("(select concat(nt.title,nt.content) from news_inf nt where nt.id=id)")
  private String comment;

映射集合属性

  • 集合元素是基本数据类型及其包装类,字符串类型和日期类型,使用@ElementCollection映射集合属性。并使用@Column映射集合元素对应的列。
  • 集合元素是组件,使用@Embeddable修饰非持久化实体的复合类。
  • 集合元素是关联的持久化实体。用@OneToMany等进行关联映射。
  @ElementCollection(targetClass=String.class)
  @CollectionTable(name="school_inf",
      joinColumns=@JoinColumn(name="person_id",nullable=false))
  @Column(name="school_name")
  @OrderColumn(name="list_order")
  private List<String> schools = new ArrayList<String>();

处理数组跟处理list类型的数据是差不多的。
处理map集合属性

@ElementCollection(targetClass=Float.class)
  @CollectionTable(name="score_inf",
      joinColumns=@JoinColumn(name="person_id",nullable=false))
  @MapKeyColumn(name="subject_name")
  @MapKeyClass(String.class)
  @Column(name="mark")
  private Map<String,Float> scores = new HashMap<String,Float>();

有序集合映射:
hibernate支持使用sortedSet和sortedMap两个有序集合。使用@SortNatural或@SortComparator排序。或者使用@OrderBy(“traning_name desc”)。

@ElementCollection(targetClass=String.class)
  @CollectionTable(name="traning_inf",
      joinColumns=@JoinColumn(name="persion_id",nullable=false))
  @Column(name="traning_name",nullable=false)
  @SortNatural
  private SortedSet<String> tranings = new TreeSet<String>();

映射数据库组件

第一种形式:在.hbm.xml文件中显示声明

<database-object>
  <create>create trigger t_full_content ..</create>
  <drop>...</drop>
</database-object>

第二种形式:提供一个实现了AuxiliaryDatabaseObject接口的类。

<database-object>
 <definition class="myTriggerDefinitioon"/>
</database-object>

SchemaExport工具类,根据映射文件生成数据库表

Configuration conf = new Configuration();
SchemaExport se = new SchemaExport(conf);
se.setFormat(true).setOutputFile(new.sql).create(true,true);

映射组件属性

@Embeddable表示作为持久化类的组件使用

public class Student {
  @Id
  @Column(name="stu_id")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;
  private Name name;

  ...}
@Embeddable
public class Name {
  @Column(name="person_firstname")
  private String first;
  @Column(name="person_lastname")
  private String last;
  @Parent
  private Student student;

也可使用另外一种方式Name不需要加任何东西而在Student哪里加;

  @Embedded
  @AttributeOverrides({
    @AttributeOverride(name="first",column=@Column(name="person_firstname")),
    @AttributeOverride(name="last",column=@Column(name="person_lastname"))
  })
  private Name name;

主键作为复合组件

要求:
* 有无参数的构造器
* 实现java.io.serializable接口
* 重写equals()和hashcode()方法。
并用

@EmbeddedId
  @AttributeOverrides({
    @AttributeOverride(name="first",
      column = @Column(name="person_firstname")),
    @AttributeOverride(name="last",
      column = @Column(name="person_lastname"))
  })
  private Name name;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值