SSH之Hibernate

Hibernate 体系结构

Hibernate 使用 Java 反射机制 而不是字节码增强程序来实现透明性。

Hibernate 的性能好,因为它是个轻量级框架。映射的灵活性很出色。

它支持各种关系数据库,从 一对一 到 多对多 的各种复杂关系。

通过注解配置Hibernate

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <property name="connection.username">root</property>
  <property name="connection.password"/>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">create</property>
  <property name="hibernate.current_session_context_class">thread</property>
 </session-factory>
</hibernate-configuration>

类级别注解

@Entity(name=”表名”)
@Table(name=”” ,catalog=”目录名”,schema=”模式”)
@Embeddable 作为另一个类的属性

属性级别注解

@Id
@GeneratedValue(strategy=GenerateType.AUTO,generator=”“)
@Column(name=”“,length=”“,unique=false)
@Embedded 标注了@Embeddable的类
@EmbeddedId
@Transient 不映射到数据库

关系级别注解

@OneToOne(cascasde=CascadeType.ALL,mappedBy=”“)
@JoinColumn(name=”id”,unique=true)

@ManyToOne
@OneToMany

@ManyToMany
@JoinTable
ps:先保存没有外键的,再保存有外键的

单向一对一

@Entity
public class Students implements Serializable{
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="pid",unique=true)
    private IDCard idCard;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int sid; 
    ...
    }

双向一对一

@Entity
public class IDCard {

    @Id
    @GeneratedValue(generator="pid")
    @GenericGenerator(name="pid",strategy="assigned")
    @Column(length=18)
    private String pid;
    private String sname;
    //双向一对一
    @OneToOne(mappedBy="idCard")
    ...
    }

单向多对一

@Entity
public class Students implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int sid;
    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="cid",referencedColumnName="CID")
    private ClassRoom classRoom;

双向多对一

@Entity
public class Students implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int sid;
    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="cid",referencedColumnName="CID")
    private ClassRoom classRoom;
    ...
    }
@Entity
public class ClassRoom {

    @Id
    @GeneratedValue(generator="cid")
    @GenericGenerator(name="cid",strategy="assigned")
    @Column(length=8)
    private String cid;

    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @JoinColumn(name="cid")
    private Set<Students> set=new HashSet<>();
    ...
    }

单向多对多

@Entity
public class Students implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int sid;
    @ManyToMany
    @JoinTable(name="student_teacher",joinColumns={@JoinColumn(name="sid")},
    inverseJoinColumns={@JoinColumn(name="id")})
    private Set<Teacher> set=new HashSet<>();
    ...
    }

双向多对多

@Entity
public class Students implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int sid;
    @ManyToMany
    @JoinTable(name="student_teacher",joinColumns={@JoinColumn(name="sid")},
    inverseJoinColumns={@JoinColumn(name="id")})
    private Set<Teacher> set=new HashSet<>();
    ...
    }
@Entity
public class Teacher {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    @ManyToMany(mappedBy="set")
    private Set<Students> set=new HashSet<>();
    ...
    }

HQL

HQL没有什么特别的地方,会SQL的话很轻松就懂怎么写,写HQL主要注意以下几点

  1. 类和属性大小写敏感
  2. 返回结果可以有List Object[] List<Map>三种
  3. 自定义的类型要有相应的构造函数
  4. 特殊的集合运算 is[not]empty ,member of.

简单示例

        Query query=session.createQuery("from Student ");
        List<Student> list=query.list();
        System.out.println("查询到的信息"+list);

你可能感兴趣的:(java,框架,Hibernate,ssh)