Hibernate 一对一关联 (多对一的曲线实现)

xml 代码
  1. CREATE TABLE AUTHOR   
  2. (   
  3.     id int(20) NOT NULL,   
  4.     name varchar(50) default NULL,   
  5.     PRIMARY KEY (id)   
  6. );   
  7.   
  8. CREATE TABLE TOPIC   
  9. (   
  10.     id int(20) NOT NULL,   
  11.     name varchar(50) default NULL,   
  12.     userId int(20) default NULL,   
  13.     PRIMARY KEY(id)   
  14. );   
  15.   
  16. ALTER TABLE TOPIC ADD CONSTRAINT FK_TOPIC_AUTHOR FOREIGN KEY userId REFERENCES AUTHOR   
xml 代码
  1. <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">  
  2.     <class name="Author" dynamic-update="true" dynamic-insert="true" table="Author">  
  3.         <id name="id" type="integer" column="id">  
  4.             <generator class="identity" />  
  5.         </id>  
  6.         <property name="name" column="username" type="string" not-null="true" length="50" />  
  7.         <one-to-one name="topic" class="Topic" cascade="none" outer-join="true" property-ref="topic"/>  
  8.     </class>  
  9. </hibernate-mapping>    
java 代码
  1. public class Author {   
  2.     private int id;   
  3.     private String name;   
  4.     private Topic topic; //做一对一的双向连接   
  5.     public Topic getTopic() {   
  6.         return topic;   
  7.     }   
  8.     public void setTopic(Topic topic) {   
  9.         this.topic = topic;   
  10.     }      
  11.     public int getId() {   
  12.         return id;   
  13.     }   
  14.     public void setId(int id) {   
  15.         this.id = id;   
  16.     }   
  17.     public String getName() {   
  18.         return name;   
  19.     }   
  20.     public void setName(String name) {   
  21.         this.name = name;   
  22.     }   
  23. }   
xml 代码
  1. <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">  
  2. <class name="Topic" dynamic-update="true" dynamic-insert="true" table="Topic">  
  3. <id name="id" type="integer" column="id">  
  4. <generator class="identity" />  
  5. </id>  
  6. <property name="name" column="username" type="string" not-null="true" length="50" />  
  7. <!-- 一对一关系连接 -->  
  8. <many-to-one name="author" class="Author" column="userid" unique="true" cascade="all" outer-join="true"/>    
  9. </class>  
  10. </hibernate-mapping>  
java 代码
  1. public class Topic {   
  2.     private int id;   
  3.     private String name;   
  4.     private Author author;   
  5.     public Author getAuthor() {   
  6.         return author;   
  7.     }   
  8.     public void setAuthor(Author author) {   
  9.         this.author = author;   
  10.     }   
  11.     public int getId() {   
  12.         return id;   
  13.     }   
  14.     public void setId(int id) {   
  15.         this.id = id;   
  16.     }   
  17.     public String getName() {   
  18.         return name;   
  19.     }   
  20.     public void setName(String name) {   
  21.         this.name = name;   
  22.     }   
  23. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值