hibernate.cfg
<?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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="connection.url">
jdbc:db2://192.168.25.230:50000/JSAMPLE
</property>
<property name="connection.username">zyl</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.ibm.db2.jcc.DB2Driver
</property>
<property name="myeclipse.connection.profile">zyl</property>
<mapping resource="com/zyl/po/User.hbm.xml" />
<mapping resource="com/zyl/po/Card.hbm.xml" />
</session-factory>
</hibernate-configuration>
----------------------------------------------------------------------
package com.zyl.po;
/**
* User generated by MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
public User(Integer userid, String username) {
super();
this.userid = userid;
this.username = username;
}
public User(Integer userid, String username, Card card) {
super();
this.userid = userid;
this.username = username;
this.card = card;
}
// Fields
private Integer userid;
private String username;
private Card card;
// 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;
}
}
----------------------------------------------------------------------
User.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" >
<hibernate-mapping>
<class name="com.zyl.po.User" table="t_user">
<id name="userid" type="integer">
<column name="userid" />
<generator class="increment" />
</id>
<property name="username" type="string">
<column name="username" length="32" not-null="true" />
</property>
<one-to-one name="card" class="com.zyl.po.Card" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
--------------------------------------------------------------------
package com.zyl.po;
/**
* Card generated by MyEclipse Persistence Tools
*/
public class Card implements java.io.Serializable {
// Fields
private Integer cardid;
private Integer cardnum;
private User user;
// Constructors
/** default constructor */
public Card() {
}
/** full constructor */
public Card(Integer cardnum) {
this.cardnum = cardnum;
}
// Property accessors
public Integer getCardid() {
return this.cardid;
}
public void setCardid(Integer cardid) {
this.cardid = cardid;
}
public Integer getCardnum() {
return this.cardnum;
}
public void setCardnum(Integer cardnum) {
this.cardnum = cardnum;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
----------------------------------------------------------------------
Card.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" >
<hibernate-mapping>
<class name="com.zyl.po.Card" table="t_card">
<id name="cardid" type="integer">
<column name="cardid" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="cardnum" type="integer">
<column name="cardnum" not-null="true" />
</property>
<one-to-one name="user" class="com.zyl.po.User"></one-to-one>
</class>
</hibernate-mapping>
--------------------------------------------------------------------
UserDAO.java
package com.zyl.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zyl.factory.HibernateSessionFactory;
import com.zyl.po.User;
public class UserDAO {
public void save(User user) {
Session session = HibernateSessionFactory.getSession();
Transaction trans = session.beginTransaction();
session.save(user);
trans.commit();
session.close();
}
}
-------------------------------------------------------------------
CardDAO.java
package com.zyl.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zyl.factory.HibernateSessionFactory;
import com.zyl.po.Card;
public class CardDAO {
public void save(Card card ) {
Session session = HibernateSessionFactory.getSession();
Transaction trans = session.beginTransaction();
session.save(card);
trans.commit();
session.close();
}
}
---------------------------------------------------------------------
package com.zyl.dao.test;
import org.junit.Assert;
import org.junit.Test;
import com.zyl.dao.UserDAO;
import com.zyl.po.Card;
import com.zyl.po.User;
public class UserDAOTestCase {
@Test
public void testSaveUsers(){
User user=new User();
user.setUsername("zyl");
Card card=new Card();
user.setCard(card);
card.setUser(user);
card.setCardnum(1);
UserDAO userDAO =new UserDAO();
userDAO.save(user);
Assert.assertNotNull("插入失败",user.getUserid());
System.out.println("user.getUsername()");
}
}
----------------------------------
表
T-USER
主键 USERID INTEGER 4 否
USERNAME VARCHAR 32 否
T-CARD
主键 CARDID INTEGER 4 否
CARDNUM INTEGER 4 否
hibernate one-to-one
原创mb6444ed45406a4 ©著作权
©著作权归作者所有:来自51CTO博客作者mb6444ed45406a4的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
hibernate中one-to-one的应用介绍
第一种:双向one-to-one,两边都配置one-to-one且数据库有主外键关系,外键又是子
hibernate one-to-one many-to-one java xml -
hibernate:one
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configur
hibernate User List -
Hibernate-one-to-Many详解
hibernate中one-tomany的基本原理,详细介绍
hibernate hql one-to -
Beginning MyBatis 3 Part 2 : How to Handle One-to-Many and One-to-One Selects
One of the latest MyBatis feature is the ability to use Annotations or XML to do One-to-One or One-to-Many queries. Let’s start with an example, as usual im using PostgreSQL, Netbeans 6.9 and MyBatis
java sql xml apache postgresql