?? hibernate入門篇之新增特性.txt
字號:
作者:smallduzi(杜恒飛)
版權聲明:本文嚴禁轉載,如有轉載請求,請和作者聯系
版本:hibernate-2.1final。
eclipse 2.1.2。
jsdk-1.4.2
所有文件存放在:com.javamodel.hibernate目錄下
hibernate在連接數據庫方面有兩種方式:
1。使用hibernate.properties文件,我下面采用的就是這種方式。這種方式比較直觀,也符合一些老程序員的思維模式。
2。使用hibernate.cfg.xml文件,這是hibernate默認的文件,在這里可以描述mapping文件,我想這就是最大的好處吧。
好,讓我們開始吧!
Example.java
package com.javamodel.hibernate;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class Example{
private static SessionFactory _sessions = null;
private static Properties pops = new Properties();
//啟動后,一次加載。
static{
try {
//這里根據個人的喜好,在這里要是著這樣的話:"/hibernate.properties",
//就可以把hibernate.properties放到src目錄下和com平級,在tomcate中放在web應用程序的web-inf/class目錄下了。
InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
try {
pops.load(stream);
} catch (IOException e1) {
e1.printStackTrace();
}
Configuration cfg = new Configuration();
//比較麻煩的地方,class需要一個一個的加載。也就是增加一個表和對應的類,那各類要在這里注冊一下。
cfg.addClass(Person.class);
cfg.setProperties(pops);
_sessions = cfg.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HibernateException {
Person person = new Person();
person.setName("smallduzi");
person.setEmail("smallduzi@sohu.com");
Session session = _sessions.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(person);
tx.commit();
}catch(HibernateException he){
if(tx != null) tx.rollback();
throw he;
}
finally{
session.close();
}
}
}
Person.java
package com.javamodel.hibernate;
public class Person {
private String id = null;
private String name = null;
private String email = null;
public Person(){}
/**
* @return
*/
public String getEmail() {
return email;
}
/**
* @return
*/
public String getId() {
return id;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param string
*/
public void setEmail(String string) {
email = string;
}
/**
* @param string
*/
public void setId(String string) {
id = string;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
}
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.javamodel.hibernate.Person" table="person">
<id name="id">
<column name="id" length="40"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" />
<property name="email" column="email" />
</class>
</hibernate-mapping>
這里我用的是oracle數據庫,如果你使用的是其他數據庫,可參照hibernate.properties中其他的數據庫連接說明。
hibernate.properties
## Oracle
hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username XXX
hibernate.connection.password XXX
#hibernate.connection.url jdbc:oracle:thin:@192.168.0.28:1521:orcl
hibernate.connection.url jdbc:oracle:oci8:@XXX
我沒有對他進行進一步的封裝。那樣初學者可能不理解。對session的封裝,可以參照hibernate文檔的HibernateUtil.java這個類(沒記錯的話)。
注意:在工程里要引用hibernate的jar。參照hibernate的文檔中有說需要的必須的jar。
作者:smallduzi(杜恒飛)
版權聲明:本文嚴禁轉載,如有轉載請求,請和作者聯系
接上一篇文章Hibernate入門篇之新增特性。我們繼續進行!
Author.java
package com.javamodel.hibernate;
public class Author{
private String id ;
private String alias = null;
private Person person = null;
/**
* @return
*/
public String getAlias() {
return alias;
}
/**
* @return
*/
public Person getPerson() {
return person;
}
/**
* @param string
*/
public void setAlias(String string) {
alias = string;
}
/**
* @param person
*/
public void setPerson(Person person) {
this.person = person;
}
/**
* @return
*/
public String getId() {
return id;
}
/**
* @param i
*/
public void setId(String i) {
id = i;
}
}
author.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.javamodel.hibernate.Author" table="author" >
<id name="id" column="id">
<!-- 這里我把author作為主表,外鍵的描述也可以在Person.hbm.xml中表述,也是可以的-->
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="alias" column="alias" />
<one-to-one name="person" class="com.javamodel.hibernate.Person" cascade="all" constrained="true" />
</class>
</hibernate-mapping>
Example.java
package com.javamodel.hibernate;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class Example{
private static SessionFactory _sessions = null;
private static Properties pops = new Properties();
static{
try {
InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
try {
pops.load(stream);
} catch (IOException e1) {
e1.printStackTrace();
}
Configuration cfg = new Configuration();
cfg.addClass(Person.class);
cfg.addClass(Author.class);
cfg.setProperties(pops);
_sessions = cfg.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HibernateException {
Person person = new Person();
person.setName("HengfeiDo");
person.setEmail("smallduzi@sohu.com");
Author author = new Author();
author.setAlias("smallduzi");
author.setPerson(person);
Session session = _sessions.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(author);
tx.commit();
System.out.println("over");
}catch(HibernateException he){
if(tx != null) tx.rollback();
throw he;
}
finally{
session.close();
}
}
}
如果大家有更好的實現方式可拿出來討論一下!
我將在下一篇文章介紹one-to-many。
參與討論
hibernate入門篇之新增特性_3:one-to-many
作者:smallduzi(杜恒飛)
版權聲明:本文嚴禁轉載,如有轉載請求,請和作者聯系
接上一篇文章hibernate入門篇之新增特性_2:one-to-one。我們再繼續進行!(為什么要說再呢?)
one to many時,兩端都需要描述。
Publication.java
package com.javamodel.hibernate;
public class Publication {
private String id = null;
private String bookName = null;
private String dataTime = null;
private String authorId = null;
private Author author = null;
public Publication(){}
/**
* @return
*/
public String getBookName() {
return bookName;
}
/**
* @return
*/
public String getDataTime() {
return dataTime;
}
/**
* @return
*/
public String getId() {
return id;
}
/**
* @param string
*/
public void setBookName(String string) {
bookName = string;
}
/**
* @param string
*/
public void setDataTime(String string) {
dataTime = string;
}
/**
* @param string
*/
public void setId(String string) {
id = string;
}
/**
* @return
*/
public String getAuthorId() {
return authorId;
}
/**
* @param string
*/
public void setAuthorId(String string) {
authorId = string;
}
/**
* @return
*/
public Author getAuthor() {
return author;
}
/**
* @param author
*/
public void setAuthor(Author author) {
this.author = author;
}
}
Publication.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.javamodel.hibernate.Publication" table="publication">
<id name="id" column="id">
<generator class="uuid.hex"/>
</id>
<property name="bookName" column="bookname" />
<property name="dataTime" column="datatime" />
<!--多的這端也需要一個簡單的many-to-one的描述-->
<many-to-one name="author" column="authorid" />
</class>
</hibernate-mapping>
Author.java加上
private Set publications = new HashSet();//add get,set
author.hbm.xml加上
<!--
這里是主的這端,你可以使用set,list,bag等,參照說明文檔,inverse="true",默認為false。這個地方
在描述這兩個對應的mapping文件的時候,必須要有一端為:true。yehs220也多次提到過。
-->
<set name="publications" lazy="true" inverse="true" cascade="all" >
<key column="authorid"/>
<one-to-many class="com.javamodel.hibernate.Publication" />
</set>
Example.java
package com.javamodel.hibernate;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class Example{
private static SessionFactory _sessions = null;
private static Properties pops = new Properties();
static{
try {
InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
try {
pops.load(stream);
} catch (IOException e1) {
e1.printStackTrace();
}
Configuration cfg = new Configuration();
cfg.addClass(Person.class);
cfg.addClass(Author.class);
cfg.addClass(Publication.class);
cfg.setProperties(pops);
_sessions = cfg.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HibernateException {
Person person = new Person();
person.setName("HengfeiDo");
person.setEmail("smallduzi@sohu.com");
Publication publication1 = new Publication();
publication1.setBookName("AAA");
publication1.setDataTime("20031224");
Publication publication2 = new Publication();
publication2.setBookName("BBB");
publication2.setDataTime("20031225");
Author author = new Author();
author.setAlias("smallduzi");
author.setPerson(person);
author.getPublications().add(publication1);
author.getPublications().add(publication2);
publication1.setAuthor(author);
publication2.setAuthor(author);
Session session = _sessions.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(author);
tx.commit();
System.out.println("over");
}catch(HibernateException he){
if(tx != null) tx.rollback();
throw he;
}
finally{
session.close();
}
}
}
下一篇,我們介紹many-to-many
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -