📄 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 + -