标签:
package org.crazyit.app.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="address_inf")
public class Address
{
// 标识属性
@Id @Column(name="address_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int addressId;
// 定义地址详细信息的成员变量
private String addressDetail;
// 无参数的构造器
public Address()
{
}
// 初始化全部成员变量的构造器
public Address(String addressDetail)
{
this.addressDetail = addressDetail;
}
// addressId的setter和getter方法
public void setAddressId(int addressId)
{
this.addressId = addressId;
}
public int getAddressId()
{
return this.addressId;
}
// addressDetail的setter和getter方法
public void setAddressDetail(String addressDetail)
{
this.addressDetail = addressDetail;
}
public String getAddressDetail()
{
return this.addressDetail;
}
}
package org.crazyit.app.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(name="person_inf")
public class Person
{
// 标识属性
@Id @Column(name="person_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private int age;
// 定义该Person实体关联的Address实体
@ManyToOne(targetEntity=Address.class)
// 映射外键列,指定外键列的列名为address_id、不允许为空
@JoinColumn(name="address_id" , nullable=false)
@Cascade(CascadeType.ALL)
private Address address;
// id的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
// name的setter和getter方法
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
// age的setter和getter方法
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
// address的setter和getter方法
public void setAddress(Address address)
{
this.address = address;
}
public Address getAddress()
{
return this.address;
}
}
package lee;
import org.crazyit.app.domain.Address;
import org.crazyit.app.domain.Person;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class PersonManager
{
public static void main(String[] args)
{
PersonManager mgr = new PersonManager();
mgr.testCascase();
HibernateUtil.sessionFactory.close();
}
private void testCascase()
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Person p = new Person();
Address a = new Address("SuZhou"); // ①
p.setName("ZhangSan");
p.setAge(21);
p.setAddress(a);
// 持久化Person对象
session.persist(p);
// 创建一个瞬态的Address对象
Address a2 = new Address("ShangHai"); // ②
// 修改持久化状态的Person对象
p.setAddress(a2); // ③
tx.commit();
HibernateUtil.closeSession();
}
private void testPerson()
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
// 创建一个Person对象
Person p = new Person();
// 设置Person的name为crazyit字符串
p.setName("WangWu");
p.setAge(21);
// 持久化Person对象(对应于插入主表记录)
session.save(p);
// 创建一个瞬态的Address对象
Address a = new Address("GuangZhou"); // ①
// 先设置Person和Address之间的关联关系
p.setAddress(a);
// 再持久化Address对象(对应于插入从表记录)
session.persist(a);
// 创建一个瞬态的Address对象
Address a2 = new Address("BeiJing"); // ②
// 先设置Person和Address之间的关联关系
p.setAddress(a2);
// 再持久化Address对象(对应于插入从表记录)
session.persist(a2); // ③
tx.commit();
HibernateUtil.closeSession();
}
}
【Hibernate学习笔记-6.1】无连接表的N-1关联(单向)
标签:
原文地址:http://www.cnblogs.com/ssslinppp/p/4547537.html