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();
}
}