关闭

Hibernate02(单项有连接表N-1)

标签: hibernatejavaspring
112人阅读 评论(0) 收藏 举报
分类:

对于大多数单项N-1关联而言,使用基于外键关联映射已经足够。但是由于底层数据建模时也可以使用连接表来建立这种关系,hibernate也提供了这种支持。

@JoinTable专门用于映射底层连接表信息

@JoinTable 支持的属性 1 name 指定连接表表名

,2 catalog, 

3 schema,

4 targetEntiy , 指定关联实体的类名

5 indexes ,注解数组 用于为该连接表定义多个索引

6 joinColumns , 该值可以接受多个@JoinColumn 用于配置连接表中外键列的信息,这些外键列参照当前实体对应表的主键列

 7 inverseJoinColumns , 该值可以接受多个@JoinColumn 

8 uniqueConstraints 连接表增加唯一约束



package com.dt.pojo;


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.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;




/**
 * 有连接表的单项N-1关联
 * 程序是需要在N的一端增加一个外键列即可
 * 
 * */












@Entity
@Table(name="person_inf")
public class Person {

@Id
@Column(name="person_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id ;
    private String name;
    private Integer age;
    
    
    @ManyToOne(targetEntity=Address.class) //定义person实体关联的address实体
    @JoinTable(name="person_address",//指定连接表的表名为address_inf
    //指定连接表中的person_id为外键,参照到实体对应表的主键列
    joinColumns=@JoinColumn(name="person_id",referencedColumnName="person_id",unique=true),
    //指定连接表中的address_id外键列,参照到当前实体的关联实体对应表的主键列
    inverseJoinColumns=@JoinColumn(name="address_id",referencedColumnName="address_id")
    
    )
    private Address address;
    
    
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}




}


注解指定连接表的表名为person_address,并指定连接表中的person_id列映射当前实体对应的主键列----由于此时映射N-1,因此还该@JoinColumn增加了unique=true,

程序无需太大修改person_inf无需增加额外的外键列,程序会使用连接表来维护person_inf表和address_inf 表之间的关联关系。


person_address 表中person_id列作为外键列参照person_inf 表的person_id主键列,也作为person_address 连接表的主键列,这也就保证了person_address数据表中的person_id列不能出现重复值,保证一个Person 实体最多能关联一个Address实体,两个实体都无需增加外键列,因此连个实体都不存在主从表的关系,




对于有关联的N-1单项链接表

必须持久化对象后方可以引用修改


package com.dt.util;


import org.hibernate.Session;
import org.hibernate.Transaction;


import com.dt.pojo.Address;
import com.dt.pojo.Person;


/*
 * N-1 有连接表
 *  一个人可以有多个地址
 *  人 ----地址
 * */


public class PersonManager
{
public static void main(String[] args)
{
PersonManager mgr = new PersonManager();
mgr.createAndStorePerson();
HibernateUtil.sessionFactory.close();
}
private void createAndStorePerson()
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();


// 创建Person对象
Person person = new Person();
person.setAge(29);
person.setName("张三");


Address a1 = new Address("广州天河");//创建一个瞬时态的Address对象
session.persist(a1);

Address a2 = new Address("上海虹口"); //创建一个瞬时态的Address对象
session.persist(a2);


person.setAddress(a1);//设置Person和Address之间的关联关系
session.persist(person);//持久化person对象

person.setAddress(a2);//修改持久化状态的Person对象


//或者这种 start

// 创建Person对象
Person person = new Person();
person.setAge(20);
person.setName("李四06");


Address a1 = new Address();//创建一个瞬时态的Address对象
a1.setAddressId(2);

person.setAddress(a1);//设置Person和Address之间的关联关系
session.persist(person);//持久化person对象

//或者这种 end



tx.commit();
HibernateUtil.closeSession();
}
}





package com.dt.pojo;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;


/**
 * 有连接表的单项N-1关联 程序是需要在N的一端增加一个外键列即可
 * 
 */


@Entity
@Table(name = "person_inf")
public class Person {


@Id
@Column(name = "person_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;


@ManyToOne(targetEntity=Address.class,fetch = FetchType.LAZY) //定义person实体关联的address实体
    @JoinTable(name="person_address",//指定连接表的表名为address_inf
    //指定连接表中的person_id为外键,参照到实体对应表的主键列
    joinColumns=@JoinColumn(name="person_id",referencedColumnName="person_id",unique=true),
    //指定连接表中的address_id外键列,参照到当前实体的关联实体对应表的主键列
    inverseJoinColumns=@JoinColumn(name="address_id",referencedColumnName="address_id"))
    private Address address;


public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public Integer getAge() {
return age;
}


public void setAge(Integer age) {
this.age = age;
}


public Address getAddress() {
return address;
}


public void setAddress(Address address) {
this.address = address;
}


}





package com.dt.pojo;


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 Integer addressId;
private String addressDetail;


public Address(String addressDetail) {
super();
this.addressDetail = addressDetail;
}


public Integer getAddressId() {
return addressId;
}


public void setAddressId(Integer addressId) {
this.addressId = addressId;
}


public String getAddressDetail() {
return addressDetail;
}


public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}


}




package com.dt.util;




import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;


public class HibernateUtil
{
public static final SessionFactory sessionFactory;


static
{
try
{
// 使用默认的hibernate.cfg.xml配置文件创建Configuration实例
Configuration cfg = new Configuration().configure();
// 以Configuration实例来创建SessionFactory实例
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}


// ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();


public static Session currentSession()
throws HibernateException
{
Session s = session.get();
// 如果该线程还没有Session,则创建一个新的Session
if (s == null)
{
s = sessionFactory.openSession();
// 将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}


public static void closeSession()
throws HibernateException
{
Session s = session.get();
if (s != null)
s.close();
session.set(null);
}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40451次
    • 积分:1760
    • 等级:
    • 排名:千里之外
    • 原创:134篇
    • 转载:62篇
    • 译文:0篇
    • 评论:4条
    最新评论