`
京华倦客
  • 浏览: 120637 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate实现one-to-one级联保存[转载]

阅读更多

库表:
CARD_TEST(CARDID[NUMBER(18)],CARDNUM[VARCHAR2(20)])
USER_TEST(USERID[NUMBER(18)],USERNAME[VARCHAR2(20)])
TestMain.java

package com.gpdi.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestMain {
/**
 * 存储一个用户
 * @param user
 */
public void save(User user){
 user.setUsername("TEST");
 Card card=new Card();
 //Card card1=new Card();
 card.setCardnum("HGJUYT");
 //card1.setCardnum("fghy55");
 user.setCard(card);
 //user.setCard(card1);
 Session session=HibernateSessionFactory.currentSession();
    card.setUser(user);
    //card1.setUser(user);
 Transaction tr=session.beginTransaction();
 session.save(user);
 //session.save(card);
 tr.commit();
 session.close(); 
}

 public static void main(String[] args) throws Exception {
  TestMain test=new TestMain();
     test.save(new User());
 }

}
Card.java

 

package com.gpdi.test;

/**
 * Card generated by MyEclipse - Hibernate Tools
 */

public class Card  implements java.io.Serializable {
    // Fields   

     private Integer cardid;
     private String cardnum;
     private User user;

    // Constructors

    public User getUser() {
  return user;
 }
 public void setUser(User user) {
  this.user = user;
 }
 /** default constructor */
    public Card() {
    }
    /** full constructor */
    public Card(String cardnum) {
        this.cardnum = cardnum;
    }
    // Property accessors

    public Integer getCardid() {
        return this.cardid;
    }
   
    public void setCardid(Integer cardid) {
        this.cardid = cardid;
    }

    public String getCardnum() {
        return this.cardnum;
    }
   
    public void setCardnum(String cardnum) {
        this.cardnum = cardnum;
    }

}

 

User.java
package com.gpdi.test;

import java.util.HashSet;
import java.util.Set;

/**
 * User generated by MyEclipse - Hibernate Tools
 */

public class User  implements java.io.Serializable {
    // Fields   

     private Integer userid;
     private String username;
     private Card card;
     //private Set card = new HashSet();

    // Constructors

    public Card getCard() {
  return card;
 }

 public void setCard(Card card) {
  this.card = card;
 }

 /** default constructor */
    public User() {
    }

    /** full constructor */
    public User(String username) {
        this.username = username;
    }

    // Property accessors

    public Integer getUserid() {
        return this.userid;
    }
   
    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return this.username;
    }
   
    public void setUsername(String username) {
        this.username = username;
    }
}
HibernateSessionFactory.java

 

package com.gpdi.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html}.
 */
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * NOTICE: Location should be on the classpath as Hibernate uses
     * #resourceAsStream style lookup for its configuration file. That
     * is place the config file in a Java package - the default location
     * is the default Java package.<br><br>
     * Examples: <br>
     * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
     * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

    /** Holds a single instance of Session */
 private static final ThreadLocal threadLocal = new ThreadLocal();

    /** The single instance of hibernate configuration */
    private static final Configuration cfg = new Configuration();

    /** The single instance of hibernate SessionFactory */
    private static org.hibernate.SessionFactory sessionFactory;

    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

  if (session == null || !session.isOpen()) {
   if (sessionFactory == null) {
    try {
     cfg.configure(CONFIG_FILE_LOCATION);
     sessionFactory = cfg.buildSessionFactory();
    } catch (Exception e) {
     System.err
       .println("%%%% Error Creating SessionFactory %%%%");
     e.printStackTrace();
    }
   }
   session = (sessionFactory != null) ? sessionFactory.openSession()
     : null;
   threadLocal.set(session);
  }

        return session;
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

    /**
     * Default constructor.
     */
    private HibernateSessionFactory() {
    }

}
Card.hbm.xml

<?xml version="1.0"?>
<!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="com.gpdi.test.Card" table="CARD_TEST">
 
        <id name="cardid" type="integer">
            <column name="CARDID" />
            <generator class="foreign" >
            <param name="property">user</param>
            </generator>
        </id>
        <property name="cardnum" type="string">
            <column name="CARDNUM" length="45" not-null="true" />
        </property>
        <!--
        foreign-key="userid"指定外键关联的字段,必须,不可缺少 ,cascade="save-update"指定怎么操作级联,非必须
        -->
       <one-to-one name="user" foreign-key="userid" class="com.gpdi.test.User" cascade="save-update"></one-to-one>
    </class>
</hibernate-mapping>

User.hbm.xml
<?xml version="1.0"?>
<!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="com.gpdi.test.User" table="USER_TEST">
        <id name="userid" type="integer">
            <column name="USERID" />
            <generator class="increment"/>
        </id>
        <property name="username" type="string">
            <column name="username" length="45" not-null="true" />
        </property>
        <one-to-one name="card" foreign-key="cardid" class="com.gpdi.test.Card" cascade="save-update"></one-to-one>
        <!--
       <set name="card" inverse="true" cascade="all">
        <key column="cardid" />
        <one-to-many class="com.gpdi.test.Card" />
        </set>
         <many-to-one  name="card" class="com.gpdi.test.Card"  insert="true" unique="true" lazy="false">
         <column name="cardid"  length="18"  />
        </many-to-one>
        -->
 
    </class>
</hibernate-mapping>

分享到:
评论

相关推荐

    hibernate many-to-one(多对一)及 cascade(级联).doc

    深入理解hibernate many-to-one(多对一)及 cascade(级联).

    NHibernate中文文档

    一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 &lt;idbag&gt; 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary ...

    最全Hibernate 参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    Hibernate+中文文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate3.2中文文档(chm格式)

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    HibernateAPI中文版.chm

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate总结

    &lt;one-to-one name="emp" class="Employees" fetch="join" constrained="true" cascade="save-update" &gt; &lt;/one-to-one&gt; &lt;/hibernate-mapping&gt; Hibernate映射一对一(通过外键实现...

    Hibernate中文详细学习文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate教程

    Hibernate参考文档 ...24.4.1. "Typed" one-to-one association 24.4.2. Composite key example 24.4.3. Content based discrimination 24.4.4. Associations on alternate keys 25. 最佳实践(Best Practices)

    Hibernate3+中文参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    Hibernate 中文 html 帮助文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    hibernate3.04中文文档.chm

    符合Java习惯的关系...24.4.1. \"Typed\" one-to-one association 24.4.2. Composite key example 24.4.3. Content based discrimination 24.4.4. Associations on alternate keys 25. 最佳实践(Best Practices)

    Hibernate参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate 教程

    一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用&lt;idbag&gt;&lt;br&gt;6.8. 双向关联(Bidirectional Associations)...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子关系 164 7.3 映射一对多双向自身...

    hibernate

    一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用&lt;idbag&gt;&lt;br&gt;6.8. 双向关联(Bidirectional Associations)...

    J2EE考试复习试题.pdf

    inverse属性可以定义在&lt;one-to-many&gt;节点或节点上。 知识点6: 级联删除 在使用Hibernate的系统中,要想在删除某个客户数据的同时删除该客户对应的所有订单数据,可以配置客户和订单关联的cascade属性为all。 知识...

Global site tag (gtag.js) - Google Analytics