hibernate集合映射学习笔记

本文介绍了一个Java POJO类——学生对象的定义及其实现,并通过Hibernate框架演示了如何将该对象保存到数据库中。文章展示了如何使用List、Set、Map等集合属性以及这些属性在对象关系映射中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

12520171102





	
		
			
			
		
		
			
		
		
		
		
			
			
				
			
			
			
				
			
		
		
		
			
				
			
			
			
				
			
		
		
		
			
				
			
			
				
			
		
		
		
			
				
			
			
			
				
			
		



	

package com.tiger.domain;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * pojo类
 * @author tiger
 * @date 2017年11月2日
 */
public class Student {
	
	private Integer id;
	private String name;
	private List books;
	private String[] skill;
	private Set hobbies;
	private Map cource;
	
	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 List getBooks() {
		return books;
	}
	public void setBooks(List books) {
		this.books = books;
	}
	public Set getHobbies() {
		return hobbies;
	}
	public void setHobbies(Set hobbies) {
		this.hobbies = hobbies;
	}
	public Map getCource() {
		return cource;
	}
	public void setCource(Map cource) {
		this.cource = cource;
	}
	public String[] getSkill() {
		return skill;
	}
	public void setSkill(String[] skill) {
		this.skill = skill;
	}
	public Student(Integer id, String name, List books, Set hobbies, Map cource,
			String[] skill) {
		super();
		this.id = id;
		this.name = name;
		this.books = books;
		this.hobbies = hobbies;
		this.cource = cource;
		this.skill = skill;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", books=" + books + ", hobbies=" + hobbies + ", cource="
				+ cource + ", skill=" + Arrays.toString(skill) + "]";
	}
}
12520171102

1、list 和 array 需要设置主键,而Map 和 set 不需要设置主键

2、外键所有集合都必须设置。package com.tiger.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.tiger.domain.Student;

public class Main {

	public static void main(String[] args) {
		Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		Student student = new Student();
		student.setName("小明");
		
		List books = new ArrayList<>();
		books.add("编程思想");
		books.add("编程思想2");
		
		String[] strings = {"敲代码","游泳"};
		
		Set hobbies = new HashSet();
		hobbies.add("打球");
		hobbies.add("看电影");
		
		Map maps = new HashMap<>();
		maps.put("高数", "23");
		maps.put("体育", "98");
		
		student.setBooks(books);
		student.setSkill(strings);
		student.setCource(maps);
		student.setHobbies(hobbies);
		
		session.save(student);
		
		transaction.commit();//事务提交
		
		List students = session.createQuery(" from Student").list();
		System.out.println(students);
		
		session.close();
		sessionFactory.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljt-tiger

thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值