(十四)Hibernate中的多表操作:单向一对一
Posted 专注改变人生。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(十四)Hibernate中的多表操作:单向一对一相关的知识,希望对你有一定的参考价值。
案例一: 注解方式实现一对一
- UserBean.java
package bean; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name = "user") public class UserBean implements Serializable { @Id private Integer userid; private String username; private String password; private String sex; @Column(name = "is_admin") private String isAdmin; @OneToOne @JoinColumn(name="userid") //一定要设置@JoinColumn,否则报错 ,表示用本表中的userid字段与cardBean中的主键关联 private CardBean cardBean; public UserBean(Integer userid, String username, String password, String sex, String isAdmin) { super(); this.userid = userid; this.username = username; this.password = password; this.sex = sex; this.isAdmin = isAdmin; } public UserBean() { } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getIsAdmin() { return isAdmin; } public void setIsAdmin(String isAdmin) { this.isAdmin = isAdmin; } public CardBean getCardBean() { return cardBean; } public void setCardBean(CardBean cardBean) { this.cardBean = cardBean; } }
- CardBean.java
package bean; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "card") public class CardBean implements Serializable { @Id @Column(name = "card_id") private Integer cardId; @Column(name = "card_name") private String cardName; public CardBean(Integer cardId, String cardName) { super(); this.cardId = cardId; this.cardName = cardName; } public CardBean() { } public Integer getCardId() { return cardId; } public void setCardId(Integer cardId) { this.cardId = cardId; } public String getCardName() { return cardName; } public void setCardName(String cardName) { this.cardName = cardName; } }
- 把含注解的bean添加到总配置文件中,
案例二: 使用映射文件xml实现一对一
- CardBean.java
package bean; /** * CardBean entity. @author MyEclipse Persistence Tools */ public class CardBean implements java.io.Serializable { // Fields private Integer cardId; private String cardName; // Constructors /** default constructor */ public CardBean() { } /** minimal constructor */ public CardBean(Integer cardId) { this.cardId = cardId; } /** full constructor */ public CardBean(Integer cardId, String cardName) { this.cardId = cardId; this.cardName = cardName; } // Property accessors public Integer getCardId() { return this.cardId; } public void setCardId(Integer cardId) { this.cardId = cardId; } public String getCardName() { return this.cardName; } public void setCardName(String cardName) { this.cardName = cardName; } }
- UserBean.java
package bean; /** * UserBean entity. @author MyEclipse Persistence Tools */ public class UserBean implements java.io.Serializable { // Fields private Integer userid; private String username; private String password; private String sex; private String isAdmin; private CardBean cardBean; // Constructors /** default constructor */ public UserBean() { } /** minimal constructor */ public UserBean(Integer userid) { this.userid = userid; } /** full constructor */ public UserBean(Integer userid, String username, String password, String sex, String isAdmin) { this.userid = userid; this.username = username; this.password = password; this.sex = sex; this.isAdmin = isAdmin; } // 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; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public String getIsAdmin() { return this.isAdmin; } public void setIsAdmin(String isAdmin) { this.isAdmin = isAdmin; } public CardBean getCardBean() { return cardBean; } public void setCardBean(CardBean cardBean) { this.cardBean = cardBean; } }
- 创建映射文件 UserBean.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="bean.UserBean" table="user" catalog="test"> <id name="userid" type="java.lang.Integer"> <column name="userid" /> <generator class="assigned"></generator> </id> <property name="username" type="java.lang.String"> <column name="username" length="100" /> </property> <property name="password" type="java.lang.String"> <column name="password" /> </property> <property name="sex" type="java.lang.String"> <column name="sex" /> </property> <property name="isAdmin" type="java.lang.String"> <column name="is_admin" /> </property> <!-- 配置单向一对一 --> <one-to-one name="cardBean" class="bean.CardBean" ></one-to-one> </class> </hibernate-mapping>
- 创建映射文件 CardBean.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="bean.CardBean" table="card" catalog="test"> <id name="cardId" type="java.lang.Integer"> <column name="card_id" /> <generator class="assigned"></generator> </id> <property name="cardName" type="java.lang.String"> <column name="card_name" /> </property> </class> </hibernate-mapping>
- 把映射文件添加到总配置文件中
以上是关于(十四)Hibernate中的多表操作:单向一对一的主要内容,如果未能解决你的问题,请参考以下文章