Article View: cn.bbs.comp.lang.java
Article #21553ֵ�ù�ע�ij־û����� hibernate
From: ¯��
Date: Thu, 27 Nov 2003 07:54
Date: Thu, 27 Nov 2003 07:54
300 lines
12873 bytes
12873 bytes
��������־û���ݵ��أ�ejb(cmp)? jdo? jdbc+sql?
������Ҫ�����ƾ���һ��ֵ�ù�ע�Ķ���-��ϵӳ����hibernate,����sourceforge
.net��һ������Ŀ��
����ּ�������Ӹ���������(��ʡʱ��)��������ο����Դ�������ĵ���
�ο�:һȺţ�˹���jdo��ejb�����ۣ�Java object models debated ����Ҳ�ᵽ�˴˿�
Դ��Ŀ��
����
��һ�����ܽӴ���һ��������
���ӹ�ϵ����
1.������
ѧϰһ���µļ���Ҫ��ʱ��ģ������ʱ����ʹ����֮������Ӧ�ù�ע��
���⣺
���ǿ����ùؼ���:"jdo ejb cmp hibernate"��google����һ�Ҷ����ǵ��������£���
��ֻҪʹ��
��hibernate���˶�������Ƴ�(��Ҳ��).
���ǵĶ���ģ�Ϳ��Ա�hibernate�ܺõ����ã���ͼ�е�Persistent Object�Ǽ�
��ҵ��ʵ�����
(Ҫ���־û��Ķ���)��ͨ��hibernate�����ij־û�����ݿ��С���������ӻ�˵��һ
�С�
2.һ��������
���ǿ���һ��Person�ࣺ
����������ݱ�����������ӣ�
��person
id(primary key) name address
000000001 ���� ����
.. �� ��
Ҫͨ��hibernateʵ����Ҫ�������²��裺
��Ҫ���ȸ�ݶ���ģ�ͱ�д��ҵ��ʵ����(��bean�淶��,����get,set����
����,������jac�ȹ������)��
����xmlӳ���ļ�������ӳ����ݿ�ķ�ʽ(��easy)�����Ϳ���ͨ����ٵ�hibernate
apiд�����������г־û�
������
2.1 дPerson��(Persistent Object),�����ͨ����û��ĩ��ͬ����ע����Ӧ�ø���be
an�Ĺ淶��Ϊÿ�������ṩget,set������
h.Person
//
// WARNING: this file has been automatically generated by JAC
// DO NOT MODIFY unless you know what you are doing
//
package h;
public class Person {
String name;
/**
Sets the value of field name.
@param name value of field name
*/
public void setName(String value) {
this.name=value;
}
/**
Gets the value of field name.
@return value of field name
*/
public String getName() {
return name;
}
String address;
/**
Sets the value of field address.
@param address value of field address
*/
public void setAddress(String value) {
this.address=value;
}
/**
Gets the value of field address.
@return value of field address
*/
public String getAddress() {
return address;
}
String id;
/**
Sets the value of field id.
@param id value of field id
*/
public void setId(String value) {
this.id=value;
}
/**
Gets the value of field id.
@return value of field id
*/
public String getId() {
return id;
}
}
��
2.2 ��д����-��ϵӳ��xml�ļ������õ�����ĸ��ӳ̶ȣ����ǿ��Գ��
������o-r ӳ�乤��һ��Ҫд����ӳ���ļ���
Person.hbm.xml ��������Ϊ:"����"+"hbm.xml" ���ҷ�����Person����ͬ��Ŀ¼��
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hiberna
te-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="h.Person">
<!--hibernate�����������id-->
<id name = "id" unsaved-value = "null">
<generator class="uuid.hex"/>
</id>
<!--Ĭ�ϰ���ı���ӳ��Ϊ��ͬ���ֵı��У���Ȼ���ǿ�������ӳ�䷽ʽ-->
<property name="name"/>
<property name="address"/>
</class>
</hibernate-mapping>
��
2.3 TestPerson ������hibernate api�����г־û�Person
h.TestPerson
package h;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
class TestPerson{
private static SessionFactory sessions;
public static void main(String[] args) throws Exception{
//���û���������xmlӳ���ļ�
Configuration conf= new Configuration()
.addClass(Person.class);
//��ɲ����sql���ļ�����ǰĿ¼������ݿ�
SchemaExport dbExport=new SchemaExport(conf);
dbExport.setOutputFile("build\\sql.txt");
dbExport.create(true, true);
sessions = conf.buildSessionFactory();
//���϶���Щ�̶���ʽ�Ļ�������
//start......
Session s = sessions.openSession();
Transaction t = s.beginTransaction();
//1.����ͨʹ�÷�ʽ��������������
Person p1=new Person();
p1.setName("����");
p1.setAddress("������������");
//2.�־û�
s.save(p1);
//��ʱp1�Ѿ���������ݿ����ҵ�
t.commit();
s.close();
}
}
��
2.4 Ϊ�����е�ȻҪ������ݿ⣺����mysql��ݿ�Ϊ���ӣ�(ֻ���Ͷ�1�μ���)
hibernate.properties ��hibernateԴ����ĸ�Ŀ¼�����ҵ����ļ�ģ�壬copy������
����ĸ�Ŀ¼��������..\h��
## MySQL
## ǰ�����˼Ҷ���������ˣ�ֻ������ݿ����Ӻ�username,password
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.url jdbc:mysql://localhost/test?useUnicode=true&charact
erEncoding=GBK
hibernate.connection.username root
hibernate.connection.password
2.5 ����TestPerson��,��mysql�й۲�create�õı�person�����е����
ok!��������Ҫ����ǰ3��:дbean��(Persistent Object)��дo-rӳ���ļ�,д��ȡ��
���ࡣ
3. ���ӹ�ϵ����
���ǻ�һ�������ӵ�������������
����������ݱ�����������ӣ�
��person
id(primary key) company_id name address
000000001 000000002 ���� ����
�� �� �� ��
��Company
id(primary key) name
000000002 ����
.. ��
��
3.1 Person(����),Company��:Persistent Object
h.Company
package h;
public class Company {
String id;
public void setId(String value) {
this.id=value;
}
public String getId() {
return id;
}
String name;
public void setName(String value) {
this.name=value;
}
public String getName() {
return name;
}
java.util.List employees=new java.util.Vector();
public void setEmployees(java.util.List value) {
this.employees=value;
}
public java.util.List getEmployees() {
return employees;
}
public void addEmployee(h.Person value) {
employees.add(value);
}
public void removeEmployee(h.Person value) {
employees.remove(value);
}
public void clearEmployees() {
employees.clear();
}
}
��
3.2 ��д����-��ϵӳ��xml�ļ�,Person.hbm.xml�վ�
Company.hbm.xml Ҳ�ŵ�h.Company����ͬĿ¼��
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hiberna
te-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="h.Company">
<!--hibernate�����������id-->
<id name = "id" unsaved-value = "null">
<generator class="uuid.hex"/>
</id>
<property name="name"/>
<!--1:n��ϵ��ӳ��-->
<list name="employees" cascade="all">
<key column="company_id"/>
<index column="posn"/>
<one-to-many class="h.Person"/>
</list>
</class>
</hibernate-mapping>
��
3.3 TestCompany ������hibernate api�����г־û�Person,Company
h.TestCompany
package h;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
class TestCompany{
private static SessionFactory sessions;
public static void main(String[] args) throws Exception{
//���û���������xmlӳ���ļ�
Configuration conf= new Configuration()
.addClass(Person.class)
.addClass(Company.class);
//��ɲ����sql���ļ�����ǰĿ¼������ݿ�
SchemaExport dbExport=new SchemaExport(conf);
dbExport.setOutputFile("build\\sql.txt");
dbExport.create(true, true);
sessions = conf.buildSessionFactory();
//���϶���Щ�̶���ʽ�Ļ�������
//start......
Session s = sessions.openSession();
Transaction t = s.beginTransaction();
//1.����ͨ��ʽ��������������
Company c=new Company();
c.setName("����");
Person p1=new Person();
p1.setName("����");
p1.setAddress("������������");
Person p2=new Person();
p2.setName("������");
p2.setAddress("�Ͻ����ӳ�");
c.addEmployee(p1);
c.addEmployee(p2);
//2.�־û�
s.save(c);
//��ʱc,p1,p2�Ѿ���������ݿ����ҵ�
t.commit();
s.close();
}
}
��
3.4 ������������ݿ��ˡ�
3.5 ����TestCompany��,��mysql�й۲�create�õı�person��Company���е����.
�ܽ
����ֻչ���˼�Ϊ��2���־û����ӣ�����1:1,1:n,m:n�ȵĶ���ģ�͵�ʵ
���뿴hibernate������ĵ���
Խ�Ǹ��ӵ�ģ��Խ�������������� ʹ��hibernate,jdo,ejb����Ķ���-��ϵӳ���м�
���ĺô����Զ���ģ�
���ڿ̿�дsql+jdbc��ͬ־�Ǹ�ЪЪ�ˣ���������ˣ�(�㲻���Լ�ȥ����������м�
���ɣ�������)
�������أ�
Person�������
Person-Company������
2�����Ӷ���Ҫhibernate��lib,����������hibernate��
�ο����ӣ�
xPetstore 3.0 Ӧ��hibernate��һ�����������.
�쳣��Ծ��hibernate��
mysql:http://www.mysql.com/
mysql��һ���ܼ��õ�ͼ�οͻ��ˣ�http://www.mysqlstudio.com/
�ҵ���һƪ����jdo������
�������ߣ�
����������������˾��
--
�Ҷ��� �q�ɨr�����㣩�q�ɨr
[m[1;32m�� ��Դ:���Ұ��Ͽ�վ bbs.nankai.edu.cn��[FROM: 202.113.227.189][m
Technical Details
Message-ID:
<49QMeL$AqJ@bbs.nankai.edu.cn>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!news.bylinux.net!maily.cic.tsinghua.edu.cn!news.happynet.org!NanKaiShow Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB X-Google-Thread: f6cb0,38e6ee338bf31595,start X-Google-Attributes: gidf6cb0,public X-Google-ArrivalTime: 2003-11-26 23:55:23 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!elnk-nf2-pas!newsfeed.earthlink.net!sjc70.webusenet.com!news.webusenet.com!news.bylinux.net!maily.cic.tsinghua.edu.cn!news.happynet.org!NanKai From: holyelle.bbs@bbs.nankai.edu.cn (¯��) Newsgroups: cn.bbs.comp.lang.java Subject: ֵ�ù�ע�ij־û����� hibernate Date: 27 Nov 2003 07:54:38 GMT Organization: �Ұ��Ͽ�վ Message-ID: <49QMeL$AqJ@bbs.nankai.edu.cn> X-Filename: Java/M.1069919637.A Lines: 300