person.sql
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[person2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[person2]
GO
CREATE TABLE [dbo].[person2] (
[id] [int] NOT NULL ,
[firstname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[lastname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[zipcode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[tel] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
hibernate2.cfg.xml
<?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="show_sql">true</property>
<property name="myeclipse.connection.profile">sqlserver2</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=mldn</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="org/lxh/hibernate/Person2.hbm.xml" />
</session-factory>
</hibernate-configuration>
Person2.hbm.xml
<?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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="org.lxh.hibernate.Person2" table="person2">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<component name="name" class="org.lxh.hibernate.Name">
<property name="firstname" type="java.lang.String">
<column name="firstname" />
</property>
<property name="lastname" type="java.lang.String">
<column name="lastname" />
</property>
</component>
<component name="contact" class="org.lxh.hibernate.Contact">
<property name="address" type="java.lang.String">
<column name="address" />
</property>
<property name="zipcode" type="java.lang.String">
<column name="zipcode" />
</property>
<property name="tel" type="java.lang.String">
<column name="tel" />
</property>
</component>
</class>
</hibernate-mapping>
Pserson2.java
package org.lxh.hibernate;
public class Person2{
// 本类要包含Name和Contact类
private int id;
private Name name;
private Contact contact;
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Contact.java
package org.lxh.hibernate;
public class Contact {
private String address;
private String zipcode;
private String tel;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
Name.java
package org.lxh.hibernate;
public class Name {
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
TestDemo.java
package org.lxh.hibernate;
import java.util.Iterator;
import java.util.List;
public class TestDemo {
public static void main(String[] args){
PersonOperate po=new PersonOperate();
Person2 p2=new Person2();
Name n=new Name();
Contact c=new Contact();
n.setFirstname("song");
n.setLastname("yunbing");
c.setAddress("www.syb.cn");
c.setZipcode("10000");
c.setTel("123456");
p2.setId(1);
p2.setName(n);
p2.setContact(c);
po.insert(p2);
List l=po.queryAll();
Iterator iter=l.iterator();
while(iter.hasNext()){
Person2 p=(Person2)iter.next();
System.out.println("ID-->"+p.getId());
System.out.println("FirstName--->"+p.getName().getFirstname());
System.out.println("LastName--->"+p.getName().getLastname());
System.out.println("Address--->"+p.getContact().getAddress());
System.out.println("Zipcode---->"+p.getContact().getZipcode());
System.out.println("Tel--->"+p.getContact().getTel());
System.out.println("---------------------------");
}
}
}
PersonOperate.java
package org.lxh.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class PersonOperate {
private Session session=null;
public PersonOperate(){
Configuration config=new Configuration().configure("hibernate2.cfg.xml");
SessionFactory factory=config.buildSessionFactory();
this.session=factory.openSession();
}
public void insert(Person2 p2){
this.session.save(p2);
this.session.beginTransaction().commit();
}
public List queryAll(){
List l=null;
String hql="from Person2 as p2";
Query q=this.session.createQuery(hql);
l=q.list();
return l;
}
}