Hibernate 使用 Java 反射机制 而不是字节码增强程序来实现透明性。
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&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没有什么特别的地方,会SQL的话很轻松就懂怎么写,写HQL主要注意以下几点
List Object[] List<Map>
三种简单示例
Query query=session.createQuery("from Student ");
List<Student> list=query.list();
System.out.println("查询到的信息"+list);