Person.java
/* * Hibernate - 关联(Association)映射 * 创建日期 2005-4-25 * @author javamxj(分享java快乐) * @link Blog: htpp://blog.csdn.net/javamxj/ * htpp://javamxj.mblogger.cn */ package javamxj.hibernate.association.one2one; /** * @hibernate.class table = "T_Person" */ public class Person { private Long id; private String username; private Address address; /** * @hibernate.id * generator-class="foreign" * @hibernate.generator-param * name="property" * value="address" */ public Long getId() {return id;} public void setId(Long id) {this.id = id;} /** * @hibernate.property * length="15" * not-null="true" */ public String getUsername() {return username;} public void setUsername(String username) {this.username = username;} /** * @hibernate.one-to-one * cascade="all" */ public Address getAddress() {return address;} public void setAddress(Address address) {this.address = address;} }
Address.java
package javamxj.hibernate.association.one2one; /** * @hibernate.class table = "T_Address" */ public class Address { private String country; private String city; private String street; private String zipCode; private Long id; public Address() {} public Address(String country, String city, String street, String zipcode) { super(); this.country = country; this.city = city; this.street = street; this.zipCode = zipcode; } /** * @hibernate.id * generator-class="hilo" * unsaved-value="null" */ public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * @hibernate.property * length = "12" */ public String getCity() {return city;} public void setCity(String city) {this.city = city;} /** * @hibernate.property * length = "12" */ public String getCountry() {return country;} public void setCountry(String country) {this.country = country;} /** * @hibernate.property * length = "6" */ public String getZipCode() {return zipCode;} public void setZipCode(String number) {this.zipCode = number;} /** * @hibernate.property * length = "12" */ public String getStreet() {return street;} public void setStreet(String street) {this.street = street;} }
Demo.java
package javamxj.hibernate.association.one2one; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; public class Demo { public static void main(String[] args) throws HibernateException { Address address1 = new Address("中国", "上海", "普陀", "200055"); Person p1 = new Person(); p1.setUsername("JavaMXJ"); p1.setAddress(address1); Address address2 = new Address("中国", "北京", "海淀", "100086"); Person p2 = new Person(); p2.setUsername("张三"); p2.setAddress(address2); SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session sess = sf.openSession(); Transaction tx= sess.beginTransaction(); sess.save(p1); sess.save(p2); tx.commit(); sess.close(); sf.close(); } }


Person.java
/* * Hibernate - 关联(Association)映射 * 创建日期 2005-4-25 * @author javamxj(分享java快乐) * @link Blog: htpp://blog.csdn.net/javamxj/ * htpp://javamxj.mblogger.cn */ package javamxj.hibernate.association.one2one; /** * @hibernate.class table = "T_Person" */ public class Person { private Long id; private String username; private Address address; /** * @hibernate.id * generator-class="hilo" * unsaved-value="null" */ public Long getId() {return id;} public void setId(Long id) {this.id = id;} /** * @hibernate.property * length="15" * not-null="true" */ public String getUsername() {return username;} public void setUsername(String username) {this.username = username;} /** * @hibernate.many-to-one * column = "fk_Address" * cascade="all" * unique="true" */ public Address getAddress() {return address;} public void setAddress(Address address) {this.address = address;} }

发表于 @ 2005年06月22日 09:40:00 | 评论( loading... ) | 举报| 收藏