<hibernate-mapping package="com.user">
?<class
??name="User"
??table="user"
?>
??<id
???name="Id"
???type="integer"
???column="id"
??>
???<generator class="native"/>
??</id>
??<property
???name="Name"
???column="name"
???type="string"
???not-null="true"
???length="20"
??/>
??<property
???name="Password"
???column="password"
???type="string"
???not-null="true"
???length="20"
??/>
??<component name="Contact" class="Contact">
???<property
???name="Email"
???column="email"
???type="string"
???not-null="false"
???length="50"
??/>
??<property
???name="Address"
???column="address"
???type="string"
???not-null="false"
???length="100"
??/>
??</component>
??
?</class>?
</hibernate-mapping>
?
3.測試
添加一個測試類:HibernateTest
package com.user;
import java.util.List;
import java.util.ListIterator;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateTest {
??? public static void main(String[] args) throws HibernateException {
??????? SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
???????
??????? //
??????? //testInsert(sessionFactory);
???????
??????? //
??????? testQuery(sessionFactory);
???????
?????????
??????? sessionFactory.close();
???????
??? }
??? public static void testInsert( SessionFactory sessionFactory )throws HibernateException {
??? ?
? ?? ?Session session = sessionFactory.openSession();
??????? Transaction tx= session.beginTransaction();
??????? User user = new User();
??????? Contact contact=new Contact();
??????? contact.setEmail("email");
??????? contact.setAddress("address");
???????
??????? user.setName("caterpillar");
??????? user.setPassword("password");
??????? user.setContact(contact);
???????
??????? session.save(user);
??????? tx.commit();
??????? session.close();
??????? System.out.println("OK!");
? ?}
???
??? public static void testQuery( SessionFactory sessionFactory )throws HibernateException {
??? ?
??? ?Session session = sessionFactory.openSession();
??????? Transaction tx= session.beginTransaction();
??????? User user = new User();
??????? Contact contact=new Contact();
?????????????????
??????? Query query=session.createQuery("from User as user");
??????? //query.setCharacter(1, 'M');
??????? List names =query.list();
??????? for(ListIterator it=names.listIterator();it.hasNext();){
????????? ?user= (User)it.next();
????????? ?System.out.println("Id: " + user.getId());
??????????? System.out.println("name: " + user.getName());
??????????? System.out.println("password: " + user.getPassword());
??????????? if(user.getContact()!=null){
??????????? ?
??????????? ?if(user.getContact().getEmail()!=null){
??????????? ??System.out.println("Email: " + user.getContact().getEmail());
??????????? ?}
??????????? ?if(user.getContact().getAddress()!=null){
??????????? ??System.out.println("Address: " + user.getContact().getAddress());
???????????????
??????????? ?}
??????????? }
???????????
???????????
???????????
??????? }
?????????
??????
??????? tx.commit();
??????? session.close();
??? ?
??? }
}