注:引入注解时,@Entity等为javax.persistence包。Cascade和CascadeType为org.hibernate.annotations包.这里用的为Hibernate5.3 与之前创建SessionFactory有些不一样,在文章后面附上代码。
双向多对一
‘多’的一方表 book,‘一’的一方表 publishers,主表为book
book实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id",nullable=false,unique=true)
public int getId() {
return id;
}
@ManyToOne(fetch=FetchType.EAGER)//fetch属性可选择项有FetchType.EAGER 和 FetchType.LAZY,前者表示关联类在主类加载时同时加载,后者表示关联类在被访问时才加载
@Cascade(value={CascadeType.SAVE_UPDATE})//指定类与类之间的级联关联,主类执行保存或更新时,关联表执行同样的操作
@JoinColumn(name="publisherId")//指定数据表book的publisherId字段作为外键与数据表publishers的主键关联
public Publishers getPublishers() {
return publishers;
publishers实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id",unique=true,nullable=false)
@OneToMany(mappedBy="publishers",fetch=FetchType.EAGER)//实现一对多的关联,mappedBy相当于设置inverse=true,表示将关联关系的主管权反转
@Cascade(value={CascadeType.DELETE})//指定级联删除
public Set<Books> getBks(){
双向一对一(基于外键)
people表和identitycard表,people表的careId字段是外键,为identitycard的主键。people为主表
people实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id",unique=true,nullable=false)
public int getId() {
return id;
}
@OneToOne(fetch=FetchType.EAGER)
@Cascade(value={CascadeType.ALL})//指定级联
@JoinColumn(name="cardId")
public People getPeople() {
return people;
}
identitycard实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id",unique=true,nullable=false)
@OneToOne(fetch=FetchType.EAGER,mappedBy="identitycard")
@Cascade(value={CascadeType.ALL})//指定级联
public PeopleZj getPeopleZj(){
双向一对一(基于主键)
peopleZj表和identitycardZj表,peopleZj为主表
peopleZj实体类
@GenericGenerator(name="generator",strategy="foreign",parameters={@Parameter(name = "property", value = "identitycardZj")})
@Id
@GeneratedValue(generator="generator")
@Column(name="id",unique=true,nullable=false)
@OneToOne(fetch=FetchType.EAGER)
@Cascade(value={CascadeType.ALL})//指定级联
@PrimaryKeyJoinColumn
identitycardZj实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id",unique=true,nullable=false)
@OneToOne(mappedBy="identitycardZj")
@Cascade(value={CascadeType.ALL})//指定级联
@PrimaryKeyJoinColumn
双向多对多
student表和course表,student为主表
student实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="studentId",nullable=false,unique=true)
@ManyToMany(fetch=FetchType.EAGER)
@Cascade(value={CascadeType.SAVE_UPDATE})
@JoinTable(name="sc",joinColumns={@JoinColumn(name="sid")},inverseJoinColumns={@JoinColumn(name="cid")})
course实体类
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="courseId",unique=true,nullable=false)
@ManyToMany(mappedBy="courses",fetch=FetchType.EAGER)
@Cascade(value={CascadeType.SAVE_UPDATE})
Hibernate5.3创建SessionFactory
StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
附上完整的HibernateSessionFactory.java代码
package com.swt.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
//指定Hibernate配置文件路径
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
//创建ThreadLocal对象
private static final ThreadLocal<Session> sessionThreadLocal = new ThreadLocal<Session>();
//创建Configuration对象
private static Configuration configuration = new Configuration();
//定义SessionFactory对象
private static SessionFactory sessionFactory;
//定义configFile属性并复制
private static String configFile = CONFIG_FILE_LOCATION;
//静态代码块来启动Hibernate,该类被加载时执行一次,用于创建SessionFactory。所以SessionFactory是线程安全的,只能被实例化一次.
static
{
try {
StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (HibernateException e) {
e.printStackTrace();
}
}
//创建无参的HibernateSessionFactory构造方法
private HibernateSessionFactory(){}
//获得SessionFactory对象(sessionFactory的get方法)
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
//重建SessionFactory
public static void rebuildSessionFactory()
{
synchronized (sessionFactory) {
try {
StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure(configFile).build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
//获得Session对象
public static Session getSession()
{
//获得ThreadLocal对象管理的session对象
Session session = sessionThreadLocal.get();
try {
//判断Session对象是否已经存在或是打开
if(session==null || !session.isOpen())
{
//如果Session对象为空或未打开,再判断sessionFactory对象是否为空
if(sessionFactory==null)
{
//如果sessionFactory为空,创建SessionFactory
rebuildSessionFactory();
}
//如果sessionFactory不为空,则打开Session
session = (sessionFactory!=null)?sessionFactory.openSession():null;
sessionThreadLocal.set(session);
}
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
return session;
}
//关闭Session对象
public static void closeSession()
{
Session session = sessionThreadLocal.get();
sessionThreadLocal.set(null);
try {
if(session!=null && session.isOpen())
{
session.close();
}
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//configFile 属性的set方法
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
//configuration 属性的get方法
public static Configuration getConfiguration() {
return configuration;
}
}
配置文件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>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于查错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">student</property>
<!--设置数据库的连接url:jdbc:mysql://localhost/student,其中localhost表示mysql服务器名称,此处为本机,student是数据库名-->
<property name="connection.url">jdbc:mysql://localhost:3306/student</property>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--连接数据库的用户名和密码-->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接哪种类型的数据库服务器。是必须的,用户配置Hibernate使用不同的数据库类型-->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。FetchSize设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
<!-- <property name="jdbc.fetch_size">50</property> -->
<!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。BatchSize越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
<!-- <property name="jdbc.batch_size">23</property> -->
<!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助-->
<!-- <property name="jdbc.use_scrollable_resultset">false</property> -->
<!--数据库连接池的大小-->
<!-- <property name="hibernate.connection.pool.size">20</property> -->
<!--指定映射文件为“com/swt/entity/Users.hbm.xml”-->
<!-- <mapping resource="com/swt/entity/Users.hbm.xml"/> -->
<mapping class="com.swt.entity.Users"/>
<mapping class="com.swt.entity.Book"/>
<mapping class="com.swt.entity.Publishers"/>
<mapping class="com.swt.entity.People"/>
<mapping class="com.swt.entity.Identitycard"/>
<mapping class="com.swt.entity.PeopleZj"/>
<mapping class="com.swt.entity.IdentitycardZj"/>
<mapping class="com.swt.entity.Student"/>
<mapping class="com.swt.entity.Course"/>