hibernate
scott
jdbc:oracle:thin:@
org
MGC
tiger
oracle
true
YearPeriodPK
package cn
import java
public class YearPeriodPK implements Serializable{
private int year;
private int period;
public int getYear() {
return year;
}
public void setYear(int year) {
this
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this
}
@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
return false;
final YearPeriodPK other = (YearPeriodPK) obj;
if (period != other
return false;
if (year != other
return false;
return true;
}
}
YearPeriod
package cn
import java
public class YearPeriod {
private YearPeriodPK yearPeriodPK;
private Calendar beginDate;
private Calendar endDate;
public YearPeriodPK getYearPeriodPK() {
return yearPeriodPK;
}
public void setYearPeriodPK(YearPeriodPK yearPeriodPK) {
this
}
public Calendar getBeginDate() {
return beginDate;
}
public void setBeginDate(Calendar beginDate) {
this
}
public Calendar getEndDate() {
return endDate;
}
public void setEndDate(Calendar endDate) {
this
}
}
YearPeriod
HibernateSessionFactory
package cn
import org
import org
import org
/**
* Configures and provides access to Hibernate sessions
* current thread of execution
* pattern
*/
public class HibernateSessionFactory {
/**
* Location of hibernate
* 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
* the location of the configuration file for the current session
*/
private static String CONFIG_FILE_LOCATION =
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
nfigure(configFile);
sessionFactory = configuration
} catch (Exception e) {
System
e
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance
* the SessionFactory if needed
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal
if (session == null || !session
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory
: null;
threadLocal
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
nfigure(configFile);
sessionFactory = configuration
} catch (Exception e) {
System
e
}
}
/**
* Close the single hibernate session instance
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal
threadLocal
if (session != null) {
session
}
}
/**
* return session factory
*
*/
public static org
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
package cn
import org
import org
public class ExportToDBCreate {
public static void main(String[] args) {
Configuration cfg = new Configuration(nfigure();
SchemaExport export = new SchemaExport(cfg);
export
}
}
InitData
package cn
import java
import org
import cn
import cn
import cn
public class InitData {
public static void main(String[] args) {
Session session = null;
try {
session = HibernateSessionFactory
session
YearPeriodPK yearPeriodPK = new YearPeriodPK();
yearPeriodPK
yearPeriodPK
YearPeriod yearPeriod = new YearPeriod();
yearPeriod
Calendar beginDate = Calendar
beginDate
Calendar endDate = Calendar
endDate
yearPeriod
yearPeriod
session
session
} catch (Exception e) {
session
e
} finally {
HibernateSessionFactory
}
}
}
LoadData
package cn
import java
import org
import cn
import cn
import cn
public class LoadData {
public static void main(String[] args) {
Session session = null;
try {
session = HibernateSessionFactory
YearPeriodPK yearPeriodPK = new YearPeriodPK();
yearPeriodPK
yearPeriodPK
YearPeriod yearPeriod = (YearPeriod) session
SimpleDateFormat sdf = new SimpleDateFormat(
System
System
} catch (Exception e) {
e
} finally {
HibernateSessionFactory
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27962.html