CREATE table t_card( id bigint identity primary key, card_no varchar(50), person_id bigint foreign key references t_user(id) on delete cascade unique )
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2012-7-19 11:40:58 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.cg.foo.TUser" table="t_user"> <id name="id" type="long"> <column name="id" /> <generator class="native" /> </id> <property name="name" type="string"> <column name="name" length="20" /> </property> <property name="nickname" type="string"> <column name="nickname" length="30" /> </property> <property name="birthday" type="timestamp"> <column name="birthday" length="23" /> </property> <set name="TCards" table="t_card" inverse="true" lazy="true" fetch="select"> <key> <column name="person_id" unique="true" /> </key> <one-to-many class="com.cg.foo.TCard" /> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2012-7-19 11:40:58 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.cg.foo.TCard" table="t_card"> <id name="id" type="long"> <column name="id" /> <generator class="native" /> </id> <many-to-one name="TUser" class="com.cg.foo.TUser" fetch="select"> <column name="person_id" unique="true" /> </many-to-one> <property name="cardNo" type="string"> <column name="card_no" length="50" /> </property> </class> </hibernate-mapping>
package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Test:
package com.cg.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tutorial.util.HibernateUtil; import org.junit.Test; import com.cg.foo.TCard; import com.cg.foo.TUser; public class TestTUser { @Test public void test() { TUser user=new TUser(); user.setBirthday(new Date()); user.setId(33); user.setName("吴xx"); user.setNickname("昵称"); Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session sess = sf.getCurrentSession(); sess.beginTransaction(); System.out.println(user.toString()); Object obj = sess.save(user); System.out.println("PSERSISTED RET VALUE:"+obj.toString()); sess.getTransaction().commit(); // sess.close(); //Auto closed by ThreadLocalSessionContext } @Test public void testUserHibernateUtil(){ TUser user=new TUser(); user.setBirthday(new Date()); user.setId(33); user.setName("张三abc~!@222222"); user.setNickname("昵称~!@22"); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); System.out.println("完成"); } @Test public void testUserCard(){ TUser user=new TUser(); user.setBirthday(new Date()); user.setId(33); user.setName("张三abc~!@222222"); user.setNickname("昵称~!@22"); TCard card=new TCard(); card.setCardNo("110"); card.setTUser(user); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(user); session.save(card); session.getTransaction().commit(); System.out.println("完成"); } @Test public void testGetUserCard(){ Session sess = HibernateUtil.getSessionFactory().getCurrentSession(); sess.beginTransaction(); Object obj = sess.get(TCard.class, 1L); TCard card = (TCard)obj; System.out.println(card.getId()); System.out.println(card.getCardNo()); System.out.println(card.getTUser().getName()); System.out.println(card.getTUser().getBirthday()); sess.getTransaction().commit(); System.out.println("done"); } @Test public void testGetCardByUser(){ Session sess = HibernateUtil.getSessionFactory().getCurrentSession(); sess.beginTransaction(); Object obj = sess.get(TUser.class, 7L); TUser user= (TUser)obj; System.out.println(user.getName()); System.out.println(user.getBirthday()); TCard card = (TCard) (user.getTCards().toArray()[0]); System.out.println(card.getId()); System.out.println(card.getCardNo()); sess.getTransaction().commit(); System.out.println("done"); } }