注解:【无连接表的】Hibernate单向1->N关联

时间:2022-08-16 13:16:48

Person与Address关联:单向1->N,【无连接表的】 (性能较低,不推荐使用!)

Person.java

package org.crazyit.app.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
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.OneToMany;
import javax.persistence.Table;

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

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

private String name;

private Integer age;

// 单向1->N:一对一关联关系
// 定义该person实体关联的address实体
@OneToMany(targetEntity = Address.class // 指定关联对象
,cascade = CascadeType.ALL// 指定级联方式 。必须先持久化关联对象address再持久化本对象person。
)
// 映射外键列,此外映射的外键列将会添加到关联实体对应的数据库中
@JoinColumn(name = "person_id", referencedColumnName = "person_id")
private Set<Address> addresses = new HashSet<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 Set<Address> getAddresses() {
return addresses;
}

public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}

public Integer getAge() {
return age;
}

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

}

Address.java

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;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name = "Address_inf")
public class Address {

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

// 地址详细
private String addressDetail;

// 无参构造方法
public Address() {
}

// 初始化全部成员变量
public Address(String addressDetail) {
this.addressDetail = addressDetail;
}

public Integer getId() {
return id;
}

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

public String getAddressDetail() {
return addressDetail;
}

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

}

运行方法:Start.java

package org.crazyit.app.service;

import org.crazyit.app.domain.Address;
import org.crazyit.app.domain.Person;
import org.crazyit.app.utils.HibernateSessionFactory;
import org.hibernate.Session;

public class Start {

/**
* (无连接表的)单向1->N。
* 性能较低,不推荐使用。
* 若程序必须采用单向1->N关系模型,也应该采用
* 有连接表的单向1->N关联
* @param args
*/
public static void main(String[] args) {

Session session = HibernateSessionFactory.currentSession();
session.beginTransaction();
// 创建person对象
Person person = new Person();
// 创建Address对象
Address address = new Address("中国上海");
// 必须先持久化address
session.persist(address);

// 设置person属性值
person.setAge(20);
person.setName("单向1->N");
// 设置person和adresss之间的关系
person.getAddresses().add(address);
// 持久化person
session.persist(person);

// 创建一个瞬时Address对象
Address address2 = new Address("中国南昌");
// 必须先持久化address2
session.persist(address2);
// 修改持久化状态的person对象
person.getAddresses().add(address2);

session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}

数据模型:

注解:【无连接表的】Hibernate单向1->N关联