知识大全 Hibernate中配置复合主键映射

Posted

篇首语:三人行,必有我师焉。择其善者而从之,其不善者而改之。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Hibernate中配置复合主键映射相关的知识,希望对你有一定的参考价值。

Hibernate中配置复合主键映射  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

   通常将复合主键单独建一个类     复合主键类必须实现java io Serializable接口 必须重写hashCode和equals方法     在映射文件中配置复合主键

  hibernate cfg xml:

  scott

  jdbc:oracle:thin:@ : :MGC

   hibernate dialect Oracle Dialect

  MGC

  tiger

  oracle jdbc driver OracleDriver

  true    YearPeriodPK java:

  package cn edu ahau mgc hibernate pojo;

  import java io Serializable;

  public class YearPeriodPK implements Serializable

  private int year;        private int period;

  public int getYear()             return year;       

  public void setYear(int year)             this year = year;       

  public int getPeriod()             return period;       

  public void setPeriod(int period)             this period = period;       

  @Override        public int hashCode()             final int prime = ;            int result = ;            result = prime * result + period;            result = prime * result + year;            return result;       

  @Override        public boolean equals(Object obj)             if (this == obj)                return true;            if (obj == null)                return false;            if (getClass() != obj getClass())                return false;            final YearPeriodPK other = (YearPeriodPK) obj;            if (period != other period)                return false;            if (year != other year)                return false;            return true;           

  YearPeriod java:

  package cn edu ahau mgc hibernate pojo;

  import java util Calendar;

  public class YearPeriod

  private YearPeriodPK yearPeriodPK;        private Calendar beginDate;        private Calendar endDate;

  public YearPeriodPK getYearPeriodPK()             return yearPeriodPK;       

  public void setYearPeriodPK(YearPeriodPK yearPeriodPK)             this yearPeriodPK = yearPeriodPK;       

  public Calendar getBeginDate()             return beginDate;       

  public void setBeginDate(Calendar beginDate)             this beginDate = beginDate;       

  public Calendar getEndDate()             return endDate;       

  public void setEndDate(Calendar endDate)             this endDate = endDate;       

  

  YearPeriod hbm xml:

  HibernateSessionFactory java:

  package cn edu ahau mgc hibernate many one factory;

  import hibernate HibernateException;    import hibernate Session;    import hibernate cfg Configuration;

  /**     * Configures and provides access to Hibernate sessions tied to the     * current thread of execution   Follows the Thread Local Session     * pattern see @link ;      */    public class HibernateSessionFactory

  /**         * Location of hibernate cfg xml file          * Location should be on the classpath as Hibernate uses         * #resourceAsStream style lookup for its configuration file          * The default classpath location of the hibernate config file is         * in the default package Use #setConfigFile() to update         * the location of the configuration file for the current session          */        private static String CONFIG_FILE_LOCATION = /hibernate cfg xml ;        private static final ThreadLocal threadLocal = new ThreadLocal();        private  static Configuration configuration = new Configuration();        private static hibernate SessionFactory sessionFactory;        private static String configFile = CONFIG_FILE_LOCATION;

  static             try                 nfigure(configFile);                sessionFactory = configuration buildSessionFactory();            catch (Exception e)                 System err                        println( %%%% Error Creating SessionFactory %%%% );                e printStackTrace();                           private HibernateSessionFactory()        

  /**         * Returns the ThreadLocal Session instance   Lazy initialize         * the SessionFactory if needed          *         *  @return Session         *  @throws HibernateException         */        public static Session getSession() throws HibernateException             Session session = (Session) threadLocal get();

  if (session == null || !session isOpen())                 if (sessionFactory == null)                     rebuildSessionFactory();                               session = (sessionFactory != null) ? sessionFactory openSession()                        : null;                threadLocal set(session);           

  return session;       

  /**         *  Rebuild hibernate session factory         *         */        public static void rebuildSessionFactory()             try                 nfigure(configFile);               sessionFactory = configuration buildSessionFactory();            catch (Exception e)                 System err                        println( %%%% Error Creating SessionFactory %%%% );                e printStackTrace();                   

  /**         *  Close the single hibernate session instance          *         *  @throws HibernateException         */        public static void closeSession() throws HibernateException             Session session = (Session) threadLocal get();            threadLocal set(null);

  if (session != null)                 session close();                   

  /**         *  return session factory         *         */        public static hibernate SessionFactory getSessionFactory()             return sessionFactory;       

  /**         *  return session factory         *         *    session factory will be rebuilded in the next call         */        public static void setConfigFile(String configFile)             HibernanfigFile = configFile;            sessionFactory = null;       

  /**         *  return hibernate configuration        *         */        public static Configuration getConfiguration()             return configuration;       

  

  ExportToDBCreate java:

  package cn edu ahau mgc hibernate export;

  import hibernate cfg Configuration;    import hibernate tool hbm ddl SchemaExport;

  public class ExportToDBCreate

  public static void main(String[] args)             Configuration cfg = new Configuration(nfigure();            SchemaExport export = new SchemaExport(cfg);           export create(true true);       

  

  InitData java:

  package cn edu ahau mgc hibernate export;

  import java util Calendar;

  import hibernate Session;

  import cn edu ahau mgc hibernate factory HibernateSessionFactory;    import cn edu ahau mgc hibernate pojo YearPeriod;    import cn edu ahau mgc hibernate pojo YearPeriodPK;

  public class InitData

  public static void main(String[] args)            Session session = null;            try                 session = HibernateSessionFactory getSession();               session beginTransaction();

  YearPeriodPK yearPeriodPK = new YearPeriodPK();               yearPeriodPK setYear( );                yearPeriodPK setPeriod( );

  YearPeriod yearPeriod = new YearPeriod();                yearPeriod setYearPeriodPK(yearPeriodPK);                Calendar beginDate = Calendar getInstance();                beginDate set( );                Calendar endDate = Calendar getInstance();                endDate set( );                yearPeriod setBeginDate(beginDate);                yearPeriod setEndDate(endDate);

  session save(yearPeriod);                session getTransaction(mit();            catch (Exception e)                session getTransaction() rollback();                e printStackTrace();           finally                 HibernateSessionFactory closeSession();                   

  

  LoadData java:

  package cn edu ahau mgc hibernate export;

  import java text SimpleDateFormat;

  import hibernate Session;

  import cn edu ahau mgc hibernate factory HibernateSessionFactory;    import cn edu ahau mgc hibernate pojo YearPeriod;    import cn edu ahau mgc hibernate pojo YearPeriodPK;

  public class LoadData

  public static void main(String[] args)             Session session = null;            try                 session = HibernateSessionFactory getSession();                YearPeriodPK yearPeriodPK = new YearPeriodPK();                yearPeriodPK setYear( );                yearPeriodPK setPeriod( );

  YearPeriod yearPeriod = (YearPeriod) session load(YearPeriod class yearPeriodPK);                SimpleDateFormat sdf = new SimpleDateFormat( yyyy MM dd );                System out println( Begin Date: + sdf format(yearPeriod getBeginDate() getTime()));                System out println( End Date: + sdf format(yearPeriod getEndDate() getTime()));

cha138/Article/program/Java/ky/201311/27962

相关参考

知识大全 Hibernate一对一 主键关联映射

Hibernate一对一主键关联映射  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 hibernate注解实现复合主键

  有时一个实体的主键可能同时为多个例如同样是之前使用的CustomerEO实体需要通过name和email来查找指定实体当且仅当name和email的值完全相同时才认为是相同的实体对象要配置这样的复

知识大全 Hibernate复合主键查询

Hibernate复合主键查询  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hibernate

知识大全 Hibernate(jpa)复合主键annotation声明方法

Hibernate(jpa)复合主键annotation声明方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起

知识大全 hibernate.cfg.xml配置文件的说明

     在Eclipse中利用HibernateSynchronizer插件进行数据库表的映射数据查询和获取数据可以节省大量时间加速Hibernat

知识大全 hibernate中自定义主键生成器

  Hibernate(目前使用的版本是)中提供了多种生成主键的方式  然而当前的这么多种生成方式未必能满足我们的要求  比如increment可以在一个hibernate实例的应用上很方便的时候但是

知识大全 hibernate关系映射

  一对象关系映射基础  对象间的基本关系  首先我简要阐明一下对象之间的基本关系在这以后UML的课程中也会深入的学习对象具有的四种基本关系  关联关系关联关系在设计模式中是被提倡优先使用于继承关系的

知识大全 hibernate中自定义主键生成器[1]

  Hibernate(目前使用的版本是)中提供了多种生成主键的方式  然而当前的这么多种生成方式未必能满足我们的要求  比如increment可以在一个hibernate实例的应用上很方便的时候但是

知识大全 Hibernate主键生成策略

Hibernate主键生成策略  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  自动增长ident

知识大全 常用Hibernate主键生成策略

常用Hibernate主键生成策略  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  今天学习到了关