映射f与它的逆映射复合的结果 Hibernate中配置复合主键映射
Hibernate中配置复合主键映射
通常将复合主键单独建一个类 复合主键类必须实现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()));
lishixinzhi/Article/program/Java/ky/201311/27962