在
還是老規矩
例子兩部分
Tclass實體及其映射
public class Tclass implements java
// Fields
private Long cid;
private String cname;
private Set students = new HashSet(
// Constructors
// Property accessors
public String toString() {
return
}
}
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<!
name=
inverse=
cascade=
lazy=
<set name=
<key>
<!
name=
not
<column name=
</key>
<!
<one
</set>
</class>
</hibernate
Student實體及其映射
public class Student implements java
// Fields
private Long sid;
private Tclass tclass;
private String sname;
// Constructors
// Property accessors
public String toString() {
return
}
}
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<!
<!
name=
class=
fetch=
select表示通過外聯接來進行查詢
join表示通過內連接來查詢
<many
class=
fetch=
<!
<column name=
</many
<property name=
<column name=
</property>
</class>
</hibernate
測試班級學生模型
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
testSave();
// testDeleteTclass();
}
public static void testSave() {
Tclass c = new Tclass();
c
Student s
Student s
s
s
s
s
c
c
Session session = HibernateSessionFactory
Transaction tx = session
session
mit();
session
}
public static void testUpdateClass() {
System
Session session = HibernateSessionFactory
Tclass c = (Tclass) session
System
c
session
}
public static void testUpdateStudent() {
System
Session session = HibernateSessionFactory
Tclass c = (Tclass) session
Student s = (Student) session
s
s
System
System
session
System
System
}
public static void testDeleteStudent() {
System
Session session = HibernateSessionFactory
Student s = (Student) session
System
System
session
session
}
public static void testDeleteTclass() {
System
Session session = HibernateSessionFactory
Tclass c = (Tclass) session
System
session
session
}
public static void testQueryClass() {
System
Session session = HibernateSessionFactory
Tclass c = (Tclass) session
System
System
}
public static void testQueryStudent() {
System
Session session = HibernateSessionFactory
Student s = (Student) session
System
System
}
}
下面是樂觀鎖的使用
Foo實體和映射文件
public class Foo implements java
// Fields
private Long pid;
private Integer version;
private String name;
// Constructors
// Property accessors
public String toString() {
return
}
}
<hibernate
<class name=
optimistic
<id name=
<column name=
<generator class=
</id>
<!
<version name=
<column name=
</version>
<property name=
<column name=
</property>
</class>
</hibernate
測試
public class TestFoo {
/**
* @param args
*/
public static void main(String[] args) {
testSave();
}
public static void testSave(){
Foo foo
Session session = HibernateSessionFactory
session
session
session
}
}
public class Bar implements java
// Fields
private Long id;
private Date timestamp;
private String name;
// Constructors
// Property accessors
public String toString() {
return
}
/**
* 排序接口方法實現
* @param o 排序對象
* @return 比較值
*/
public int compareTo(Object o) {
Bar bar = (Bar) o;
Long res = this
return res
}
}
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<version name=
<column name=
</version>
<property name=
<column name=
</property>
</class>
</hibernate
public class TestBar {
public static void main(String args[]) {
testUpdateBar();
testQueryBar();
}
public static void testSaveBar() {
Bar bar = new Bar(
Session session = HibernateSessionFactory
session
session
session
}
public static void testQueryBar() {
Session session = HibernateSessionFactory
String hql =
Query query = session
List<Bar> barList = query
Collections
for (Bar bar : barList) {
System
}
session
}
public static void testUpdateBar() {
Session session = HibernateSessionFactory
String hql =
Query query = session
List<Bar> barList = query
for (Bar bar : barList) {
bar
}
session
session
}
}
public class TestStack {
public static void main(String args[]){
test();
}
public static void test(){
Stack stack = new Stack();
String s
String s
String s
String s
stack
stack
stack
stack
for(;!stack
System
}
//for語句先判斷是否符合條件
for(int i=
System
}
}
}
下面是SessionFactory工具和hibernate配置文件
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<Session> threadLocal = new ThreadLocal<Session>();
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 <code>SessionFactory</code> 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;
}
}
<?xml version=
<!DOCTYPE hibernate
<!
<hibernate
<session
<property name=
<property name=
jdbc:mysql://localhost:
</property>
<property name=
org
</property>
<property name=
com
</property>
<property name=
<property name=
com
</property>
<property name=
<!
<property name=
<mapping resource=
<mapping resource=
<mapping resource=
<mapping resource=
</session
</hibernate
數據庫用的是mysql
/*
SQLyog Enterprise
MySQL
*********************************************************************
*/
/*!
/*!
/*!
/*!
create database if not exists testdb;
USE testdb;
/*Table structure for table bar */
DROP TABLE IF EXISTS bar;
CREATE TABLE bar (
id bigint(
timestamp datetime NOT NULL
name varchar(
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
/*Table structure for table foo */
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
pid bigint(
version int(
name varchar(
PRIMARY KEY (pid)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
/*Table structure for table student */
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid bigint(
fk_cid bigint(
sname varchar(
PRIMARY KEY (sid)
KEY FK
CONSTRAINT FK
) ENGINE=InnoDB AUTO_INCREMENT=
/*Table structure for table tclass */
DROP TABLE IF EXISTS tclass;
CREATE TABLE tclass (
cid bigint(
cname varchar(
PRIMARY KEY (cid)
) ENGINE=InnoDB AUTO_INCREMENT=
/*!
/*!
具體測試運行的結果運行下即可看到
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27943.html