xml 代码
- CREATE TABLE AUTHOR
- (
- id int(20) NOT NULL,
- name varchar(50) default NULL,
- PRIMARY KEY (id)
- );
- CREATE TABLE TOPIC
- (
- id int(20) NOT NULL,
- name varchar(50) default NULL,
- userId int(20) default NULL,
- PRIMARY KEY(id)
- );
- ALTER TABLE TOPIC ADD CONSTRAINT FK_TOPIC_AUTHOR FOREIGN KEY userId REFERENCES AUTHOR
xml 代码
- <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">
- <class name="Author" dynamic-update="true" dynamic-insert="true" table="Author">
- <id name="id" type="integer" column="id">
- <generator class="identity" />
- </id>
- <property name="name" column="username" type="string" not-null="true" length="50" />
- <one-to-one name="topic" class="Topic" cascade="none" outer-join="true" property-ref="topic"/>
- </class>
- </hibernate-mapping>
java 代码
- public class Author {
- private int id;
- private String name;
- private Topic topic; //做一对一的双向连接
- public Topic getTopic() {
- return topic;
- }
- public void setTopic(Topic topic) {
- this.topic = topic;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
xml 代码
- <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">
- <class name="Topic" dynamic-update="true" dynamic-insert="true" table="Topic">
- <id name="id" type="integer" column="id">
- <generator class="identity" />
- </id>
- <property name="name" column="username" type="string" not-null="true" length="50" />
- <!-- 一对一关系连接 -->
- <many-to-one name="author" class="Author" column="userid" unique="true" cascade="all" outer-join="true"/>
- </class>
- </hibernate-mapping>
java 代码
- public class Topic {
- private int id;
- private String name;
- private Author author;
- public Author getAuthor() {
- return author;
- }
- public void setAuthor(Author author) {
- this.author = author;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }