JPA映射关系

单向

一对一

实体:

@Entity
@Table(name="tb_product")
public class Product {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	private String name;
	@OneToOne
	@JoinColumn(name="dir_id")
	private ProductDir dirId;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return the dirId
	 */
	public ProductDir getDirId() {
		return dirId;
	}
	/**
	 * @param dirId the dirId to set
	 */
	public void setDirId(ProductDir dirId) {
		this.dirId = dirId;
	}
	/** 
	 * @Title: toString
	 * @author zjh
	 * @Desc: 
	 * @return 
	 * @see java.lang.Object#toString() 
	 */
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", dirId=" + dirId
				+ "]";
	}
}

@Entity
@Table(name="tb_productDir")
public class ProductDir {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	private String name;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/** 
	 * @Title: toString
	 * @author zjh
	 * @Desc: 
	 * @return 
	 * @see java.lang.Object#toString() 
	 */
	@Override
	public String toString() {
		return "ProductDir [id=" + id + ", name=" + name + "]";
	}
}

测试

public class TestDemo {
	IProductDao iProductDao = new ProductDaoImpl();
	IProductDirDao iProductDirDao =  new ProductDirDaoImpl();
	@org.junit.Test
	public void Test(){
		ProductDir productDir = new ProductDir();
		productDir.setName("食品");
		Product product = new Product();
		product.setName("牛肉");
		product.setDirId(productDir);
		
		iProductDirDao.save(productDir);
		iProductDao.save(product);
	}
}

控制台打印

Hibernate: insert into tb_productDir (name) values (?)
Hibernate: insert into tb_product (dir_id, name) values (?, ?)

多对一

一的实体


一对多

一的实体

@Entity
public class Teacher {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String name;
	@OneToMany
	//这里如果是要一对多关系模式的话,必须加上JoinColumn()注解,否则会生成一张关联表,那样的话就会被误解为多对多
	@JoinColumn(name="student_id")
	private List<Student> students = new ArrayList<>();
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the students
	 */
	public List<Student> getStudents() {
		return students;
	}
	/**
	 * @param students the students to set
	 */
	public void setStudents(List<Student> students) {
		this.students = students;
	}
	/** 
	 * @Title: toString
	 * @author zjh
	 * @Desc: 
	 * @return 
	 * @see java.lang.Object#toString() 
	 */
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
}

多的实体

@Entity
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String name;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/** 
	 * @Title: toString
	 * @author zjh
	 * @Desc: 
	 * @return 
	 * @see java.lang.Object#toString() 
	 */
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
}

测试代码

public class OneToManyTest {
	@Test
	public void testAdd(){
		Student student = new Student();
		student.setName("tom");
		Student student2 = new Student();
		student2.setName("jreey");
		
		Teacher teacher = new Teacher();
		teacher.setName("jack");
		teacher.getStudents().add(student);
		teacher.getStudents().add(student2);
		
		EntityManagerFactory entityManagerFactory = JPAUtils.getEntityManagerFactory();
		EntityManager createEntityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = createEntityManager.getTransaction();
		transaction.begin();
		createEntityManager.persist(teacher);
		createEntityManager.persist(student);
		createEntityManager.persist(student2);
		
		transaction.commit();
		createEntityManager.close();
	}
}

日志

Hibernate: insert into Teacher (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: update Student set student_id=? where id=?
Hibernate: update Student set student_id=? where id=?

双向

一对多/多对一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值