一对一的关系在数据库中表示为主外关系.在Hibernate中的一对一关系通过映射实现。例如.人和身份证的关系.每个人都对应一个身份证号.我们应该两个表.一个是关于人信息的表(user).别外一个是身份证相关信息的表(card).card表的主键对应该user表的主键userid,也是user表的外键.有人才能有身份证.
Users表
CREATE TABLE `users` ( `user_id` int(11) NOT NULL auto_increment, `user_name` varchar(20) default NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; |
Card 表
CREATE TABLE `card` ( `card_id` int(11) NOT NULL auto_increment, `card_no` varchar(20) default NULL, `user_id` int(11) default NULL, PRIMARY KEY (`card_id`), KEY `user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; |
JAVA 代码
Users 类
package org.yy.hbn.pojo; public class Users implements java.io.Serializable { private Integer userId; private String userName; private Card card; //Users对象中存一个Card 对象 public Users() { } public Users(String userName) { this.userName = userName; } 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; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } public Users(Integer userId, String userName, Card card) { super(); this.userId = userId; this.userName = userName; this.card = card; } } |
Card 类
package org.yy.hbn.pojo; public class Card implements java.io.Serializable { private Integer cardId; private String cardNo; private Users users; //Card对象中存一个Users 对象 public Card(Integer cardId, String cardNo, Users users) { super(); this.cardId = cardId; this.cardNo = cardNo; this.users = users; } public Card() { } public Integer getCardId() { return this.cardId; } public void setCardId(Integer cardId) { this.cardId = cardId; } public String getCardNo() { return this.cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public Users getUsers() { return users; } public void setUsers(Users users) { this.users = users; } } |
XML 代码
Card.hbn.xml
Users.hbn.xml
hibernate.cfg.xml 代码
JAVA 测试代码
package org.yy.hbn.test; import org.hibernate.Transaction; import org.hibernate.Session; import org.yy.hbn.HibernateSessionFactory; import org.yy.hbn.pojo.Card; import org.yy.hbn.pojo.Users; public class test { public static void main(String[] args) { select(); } private static void add() { Session session=HibernateSessionFactory.getSession(); Transaction tran=session.beginTransaction(); try { Users user=new Users(); user.setUserName("UU"); Card card=new Card(); card.setCardNo("89012345"); //卡跟用户建立关系 card.setUsers(user); //用户也跟卡建立关系 user.setCard(card); session.save(user); tran.commit(); } catch (Exception e) { e.printStackTrace(); tran.rollback(); }finally{ HibernateSessionFactory.closeSession(); } } private static void select() { Session session=HibernateSessionFactory.getSession(); //查找用户信息,可以通过用户来找到对应的卡的信息 Users user=(Users)session.get(Users.class,new Integer(1)); System.out.println(user.getUserName()); System.out.println(user.getCard().getCardNo()); |
【编辑推荐】
- 层层解析Hibernate中的事务的滥用
- 解析Hibernate中的事务管理
- 解析Hibernate的批量删除功能
- Hibernate、Spring和Struts工作原理及使用理由