hibernate annotations注解实体_hibernate常用注解

2020-02-27 其他范文 下载本文

hibernate annotations注解实体由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“hibernate常用注解”。

hibernate annotations注解实体总结

前言

一般我们使用hibernate来存储数据时有两种方式。第一、写实体关系映射文件。写在与实体类同一目录下,用类名.hbm.xml命名。第二种是写annotations注解实体。

第一种方式比较简单,这里就不说。主要讲用annotations来注解实体时,一些位置要注意的。

第一. Hibernate.cfg.xml

“-//Hibernate/Hibernate Configuration DTD 3.0//EN”

“http://www.daodoc.com/dtd/hibernate-configuration-3.0.dtd”>

com.mysql.jdbc.Driver

jdbc:mysql://localhost/autoreplier

root

acm337

1

org.hibernate.dialect.MySQLDialect

thread

org.hibernate.cache.internal.NoCacheProvider

true

update

第二、java类

package com.autoreplier.model;

import javax.persistence.Column;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Entity;

@Entity

@Table(name=“_problem”)

publiccla Problem {

privateintid;private String content;private String sulotion;@Id @GeneratedValue(strategy=GenerationType.IDENTITY)publicint getId(){} publicvoid setId(int id){} @Column(name=“content”,nullable=true)public String getContent(){} publicvoid setContent(String content){} this.content = content;returncontent;this.id = id;returnid;

} public String getSulotion(){} publicvoid setSulotion(String sulotion){} this.sulotion = sulotion;returnsulotion;

1.@Entity声明这个pojo是一个实体bean

2.@Table注解可以为实体bean映射指定表,如果没有定义@Table,则系统自动使用默认值:实体的短类名(不附带包名),name属性的值为对应在数据库中的表名。

3.@Id注解可以将实体bean中的某个属性定义为标识符,即主键。

4.@GeneratedValue注解定义标识符的生成策略。有以下4种策略

1)AUTO ——可以是indentity column列类型,或者sequence类型或者table类型,取决于不同的底层数据库

2)TABLE——使用表保存id值

3)IDENTITY ——indentity column

4)SEQUENCE ——sequence

5.@Column注解可以将属性映射到列,使用该注解来覆盖默认值,其中,name属性表示该属性映射的列的名字,length表示该列的长度。nullable表示是否可以为空,默认为true,不能为空。

第三,工具类

使用单例模式。

package com.autoreplier.util;

import org.hibernate.SeionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

publiccla HibernateUtil {

privatestatic SeionFactory sf;static {} publicstatic SeionFactory getSeionFactory(){}returnsf;sf = new AnnotationConfiguration().configure().buildSeionFactory();

}

《hibernate annotations注解实体.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
hibernate annotations注解实体
点击下载文档
相关专题 hibernate常用注解 注解 实体 Hibernate hibernate常用注解 注解 实体 Hibernate
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文