[size=xx-large]第一步:在数据库中建立三个表:[/size]STUDENT ,BOOK,NUMS
-- Create table
create table STUDENT
(
STUDENTID NUMBER not null,
STUDENTNAME VARCHAR2(20)
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table STUDENT
add constraint W1 primary key (STUDENTID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create table
create table NUMS
(
BOOKID NUMBER,
STUDENTID NUMBER,
TUSHULIANG NUMBER,
JIESHULIANG NUMBER,
ID NUMBER not null
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table NUMS
add constraint Q5 primary key (ID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
alter table NUMS
add constraint Q1 foreign key (BOOKID)
references BOOK (BOOKID);
alter table NUMS
add constraint Q2 foreign key (STUDENTID)
references STUDENT (STUDENTID);
-- Create table
create table BOOK
(
BOOKID NUMBER not null,
BOOKNAME VARCHAR2(20)
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table BOOK
add constraint A3 primary key (BOOKID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
[size=xx-large]第二步:写三个PO对象[/size]package po;
import java.util.HashSet;
import java.util.Set;
/**
* Student generated by MyEclipse Persistence Tools
*/
public class Student implements java.io.Serializable {
// Fields
private Long studentid;
private String studentname;
private Set nums = new HashSet(0);
private Set numses = new HashSet(0);
// Constructors
/** default constructor */
public Student() {
}
/** minimal constructor */
public Student(Long studentid) {
this.studentid = studentid;
}
/** full constructor */
public Student(Long studentid, String studentname, Set nums, Set numses) {
this.studentid = studentid;
this.studentname = studentname;
this.nums = nums;
this.numses = numses;
}
// Property accessors
public Long getStudentid() {
return this.studentid;
}
public void setStudentid(Long studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return this.studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public Set getNums() {
return this.nums;
}
public void setNums(Set nums) {
this.nums = nums;
}
public Set getNumses() {
return this.numses;
}
public void setNumses(Set numses) {
this.numses = numses;
}
}
package po;
import java.util.HashSet;
import java.util.Set;
/**
* Book generated by MyEclipse Persistence Tools
*/
public class Book implements java.io.Serializable {
// Fields
private Long bookid;
private String bookname;
private Set nums = new HashSet(0);
private Set numses = new HashSet(0);
// Constructors
/** default constructor */
public Book() {
}
/** minimal constructor */
public Book(Long bookid) {
this.bookid = bookid;
}
/** full constructor */
public Book(Long bookid, String bookname, Set nums, Set numses) {
this.bookid = bookid;
this.bookname = bookname;
this.nums = nums;
this.numses = numses;
}
// Property accessors
public Long getBookid() {
return this.bookid;
}
public void setBookid(Long bookid) {
this.bookid = bookid;
}
public String getBookname() {
return this.bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Set getNums() {
return this.nums;
}
public void setNums(Set nums) {
this.nums = nums;
}
public Set getNumses() {
return this.numses;
}
public void setNumses(Set numses) {
this.numses = numses;
}
}
package po;
/**
* Nums generated by MyEclipse Persistence Tools
*/
public class Nums implements java.io.Serializable {
// Fields
private Long id;
private Book book;
private Student student;
private Long tushuliang;
private Long jieshuliang;
// Constructors
/** default constructor */
public Nums() {
}
/** minimal constructor */
public Nums(Long id) {
this.id = id;
}
/** full constructor */
public Nums(Long id, Book book, Student student, Long tushuliang,
Long jieshuliang) {
this.id = id;
this.book = book;
this.student = student;
this.tushuliang = tushuliang;
this.jieshuliang = jieshuliang;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Book getBook() {
return this.book;
}
public void setBook(Book book) {
this.book = book;
}
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
public Long getTushuliang() {
return this.tushuliang;
}
public void setTushuliang(Long tushuliang) {
this.tushuliang = tushuliang;
}
public Long getJieshuliang() {
return this.jieshuliang;
}
public void setJieshuliang(Long jieshuliang) {
this.jieshuliang = jieshuliang;
}
}
[size=xx-large]第三步:分别写三个数据库中表的字段和PO对象的映射文件:[/size]<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Student" table="STUDENT" schema="SCOTT">
<id name="studentid" type="java.lang.Long">
<column name="STUDENTID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="studentname" type="java.lang.String">
<column name="STUDENTNAME" length="20" />
</property>
<set name="nums" inverse="true">
<key>
<column name="STUDENTID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
<set name="numses" inverse="true">
<key>
<column name="STUDENTID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Book" table="BOOK" schema="SCOTT">
<id name="bookid" type="java.lang.Long">
<column name="BOOKID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="bookname" type="java.lang.String">
<column name="BOOKNAME" length="20" />
</property>
<set name="nums" inverse="true" cascade="delete">
<key>
<column name="BOOKID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
<set name="numses" inverse="true">
<key>
<column name="BOOKID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="po.Nums" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Nums" table="NUMS" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="book" class="po.Book" fetch="select">
<column name="BOOKID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="student" class="po.Student" fetch="select">
<column name="STUDENTID" precision="22" scale="0" />
</many-to-one>
<property name="tushuliang" type="java.lang.Long">
<column name="TUSHULIANG" precision="22" scale="0" />
</property>
<property name="jieshuliang" type="java.lang.Long">
<column name="JIESHULIANG" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
[size=xx-large]第四步:hibernate.cfg.xml文件中把三个映射文件加入,进行调和。[/size]<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:ora
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="po/Student.hbm.xml" />
<mapping resource="po/Book.hbm.xml" />
<mapping resource="po/Nums.hbm.xml" />
</session-factory>
</hibernate-configuration>
[size=xx-large]
最后一步:在Main 方法中测试:[/size]import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import po.*;
public class Test {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Nums n = (Nums) session.get(Nums.class, new Long(2));
System.out.println(n.getStudent().getStudentname() + " "
+ n.getBook().getBookname() + " " + n.getJieshuliang()
+ " " + n.getTushuliang());
Student s = (Student) session.get(Student.class, new Long(1));
Set set = s.getNums();
Iterator it = set.iterator();
while (it.hasNext()) {
Nums num=(Nums) it.next() ;
System.out.println( num.getTushuliang()+" "+num.getJieshuliang()+" "+num.getBook().getBookname()+" "+num.getStudent().getStudentname());
}
System.out.println("*************************");
Set set2 = s.getNumses();
Iterator it2 = set2.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
}
}
-- Create table
create table STUDENT
(
STUDENTID NUMBER not null,
STUDENTNAME VARCHAR2(20)
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table STUDENT
add constraint W1 primary key (STUDENTID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create table
create table NUMS
(
BOOKID NUMBER,
STUDENTID NUMBER,
TUSHULIANG NUMBER,
JIESHULIANG NUMBER,
ID NUMBER not null
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table NUMS
add constraint Q5 primary key (ID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
alter table NUMS
add constraint Q1 foreign key (BOOKID)
references BOOK (BOOKID);
alter table NUMS
add constraint Q2 foreign key (STUDENTID)
references STUDENT (STUDENTID);
-- Create table
create table BOOK
(
BOOKID NUMBER not null,
BOOKNAME VARCHAR2(20)
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table BOOK
add constraint A3 primary key (BOOKID)
using index
tablespace SYSTEM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
[size=xx-large]第二步:写三个PO对象[/size]package po;
import java.util.HashSet;
import java.util.Set;
/**
* Student generated by MyEclipse Persistence Tools
*/
public class Student implements java.io.Serializable {
// Fields
private Long studentid;
private String studentname;
private Set nums = new HashSet(0);
private Set numses = new HashSet(0);
// Constructors
/** default constructor */
public Student() {
}
/** minimal constructor */
public Student(Long studentid) {
this.studentid = studentid;
}
/** full constructor */
public Student(Long studentid, String studentname, Set nums, Set numses) {
this.studentid = studentid;
this.studentname = studentname;
this.nums = nums;
this.numses = numses;
}
// Property accessors
public Long getStudentid() {
return this.studentid;
}
public void setStudentid(Long studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return this.studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public Set getNums() {
return this.nums;
}
public void setNums(Set nums) {
this.nums = nums;
}
public Set getNumses() {
return this.numses;
}
public void setNumses(Set numses) {
this.numses = numses;
}
}
package po;
import java.util.HashSet;
import java.util.Set;
/**
* Book generated by MyEclipse Persistence Tools
*/
public class Book implements java.io.Serializable {
// Fields
private Long bookid;
private String bookname;
private Set nums = new HashSet(0);
private Set numses = new HashSet(0);
// Constructors
/** default constructor */
public Book() {
}
/** minimal constructor */
public Book(Long bookid) {
this.bookid = bookid;
}
/** full constructor */
public Book(Long bookid, String bookname, Set nums, Set numses) {
this.bookid = bookid;
this.bookname = bookname;
this.nums = nums;
this.numses = numses;
}
// Property accessors
public Long getBookid() {
return this.bookid;
}
public void setBookid(Long bookid) {
this.bookid = bookid;
}
public String getBookname() {
return this.bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Set getNums() {
return this.nums;
}
public void setNums(Set nums) {
this.nums = nums;
}
public Set getNumses() {
return this.numses;
}
public void setNumses(Set numses) {
this.numses = numses;
}
}
package po;
/**
* Nums generated by MyEclipse Persistence Tools
*/
public class Nums implements java.io.Serializable {
// Fields
private Long id;
private Book book;
private Student student;
private Long tushuliang;
private Long jieshuliang;
// Constructors
/** default constructor */
public Nums() {
}
/** minimal constructor */
public Nums(Long id) {
this.id = id;
}
/** full constructor */
public Nums(Long id, Book book, Student student, Long tushuliang,
Long jieshuliang) {
this.id = id;
this.book = book;
this.student = student;
this.tushuliang = tushuliang;
this.jieshuliang = jieshuliang;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Book getBook() {
return this.book;
}
public void setBook(Book book) {
this.book = book;
}
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
public Long getTushuliang() {
return this.tushuliang;
}
public void setTushuliang(Long tushuliang) {
this.tushuliang = tushuliang;
}
public Long getJieshuliang() {
return this.jieshuliang;
}
public void setJieshuliang(Long jieshuliang) {
this.jieshuliang = jieshuliang;
}
}
[size=xx-large]第三步:分别写三个数据库中表的字段和PO对象的映射文件:[/size]<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Student" table="STUDENT" schema="SCOTT">
<id name="studentid" type="java.lang.Long">
<column name="STUDENTID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="studentname" type="java.lang.String">
<column name="STUDENTNAME" length="20" />
</property>
<set name="nums" inverse="true">
<key>
<column name="STUDENTID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
<set name="numses" inverse="true">
<key>
<column name="STUDENTID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Book" table="BOOK" schema="SCOTT">
<id name="bookid" type="java.lang.Long">
<column name="BOOKID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="bookname" type="java.lang.String">
<column name="BOOKNAME" length="20" />
</property>
<set name="nums" inverse="true" cascade="delete">
<key>
<column name="BOOKID" precision="22" scale="0" />
</key>
<one-to-many class="po.Nums" />
</set>
<set name="numses" inverse="true">
<key>
<column name="BOOKID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="po.Nums" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Nums" table="NUMS" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="book" class="po.Book" fetch="select">
<column name="BOOKID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="student" class="po.Student" fetch="select">
<column name="STUDENTID" precision="22" scale="0" />
</many-to-one>
<property name="tushuliang" type="java.lang.Long">
<column name="TUSHULIANG" precision="22" scale="0" />
</property>
<property name="jieshuliang" type="java.lang.Long">
<column name="JIESHULIANG" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
[size=xx-large]第四步:hibernate.cfg.xml文件中把三个映射文件加入,进行调和。[/size]<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:ora
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="po/Student.hbm.xml" />
<mapping resource="po/Book.hbm.xml" />
<mapping resource="po/Nums.hbm.xml" />
</session-factory>
</hibernate-configuration>
[size=xx-large]
最后一步:在Main 方法中测试:[/size]import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import po.*;
public class Test {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Nums n = (Nums) session.get(Nums.class, new Long(2));
System.out.println(n.getStudent().getStudentname() + " "
+ n.getBook().getBookname() + " " + n.getJieshuliang()
+ " " + n.getTushuliang());
Student s = (Student) session.get(Student.class, new Long(1));
Set set = s.getNums();
Iterator it = set.iterator();
while (it.hasNext()) {
Nums num=(Nums) it.next() ;
System.out.println( num.getTushuliang()+" "+num.getJieshuliang()+" "+num.getBook().getBookname()+" "+num.getStudent().getStudentname());
}
System.out.println("*************************");
Set set2 = s.getNumses();
Iterator it2 = set2.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
}
}