Hibernate5.3基础配置,创建SessionFactory和关联映射的注解

注:引入注解时,@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 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 sessionThreadLocal = new ThreadLocal();
    //创建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




  
  
  true
  student
  
  jdbc:mysql://localhost:3306/student
   
  com.mysql.jdbc.Driver
  
  root
  123456
   
  org.hibernate.dialect.MySQLInnoDBDialect

    
   

    
  

    
  

    
  


   
  
  
  
  
  
  
  
  
  
  


你可能感兴趣的:(Hibernate)