//  Student.java 实体类package com.tao.pojo;import java.util.List;public class Student {private int id;private String name;private String gender;private int age;//list集合private List<String> hobby;    //Map 集合//private Map<Integer,String> hobby=new HashMap<Integer,String>();public Student() {super();}public Student(int id, String name, String gender, int age) {super();this.id = id;this.name = name;this.gender = gender;this.age = age;}public Student(String name, String gender, int age, List<String> hobby) {super();this.name = name;this.gender = gender;this.age = age;this.hobby = hobby;}public Student(int id, String name, String gender, int age, List<String> hobby) {super();this.id = id;this.name = name;this.gender = gender;this.age = age;this.hobby = hobby;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<String> getHobby() {return hobby;}public void setHobby(List<String> hobby) {this.hobby = hobby;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";}    }//Student.hbm.xml 映射文件<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.schemaupdate"><class name="com.tao.pojo.Student" table="student"><id name="id" column="id"></id><property name="name" column="name" type="string"/>    <property name="age" column="age"/>    <list name="hobby" table="stu_hobby"><!-- 外键,自己起名 --><key column="sid"></key><!--索引列  --><list-index column="stu_index"></list-index><!--其他普通列  --><element column="descx" type="string"></element></list>
<!--map集合  -->
<!-- <map name="hobby" table="bobby"><key column="sid"></key><index column="stu_id" type="int"></index><element column="descx" type="string"></element></map> --></class></hibernate-mapping>// hibernate.cfg.xml  配置文件<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><!-- 数据库连接部分 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/test0111_list?characterEncoding=utf-8</property><property name="connection.username">root</property><property name="connection.password">root</property><!--显示SQl语句  --><property name="show_sql">true</property><property name="format_sql">true</property><!--MySQL数据库方言  --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 自动创建或生成表--><property name="hbm2ddl.auto">update</property><!--映射文件  --><mapping resource="com/tao/pojo/Student.hbm.xml"/>        </session-factory></hibernate-configuration>// Test.java 测试类package com.tao.test;import java.util.ArrayList;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MySQL5Dialect;import com.tao.pojo.Student;public class Test {public static void main(String[] args) {Configuration configure = new Configuration().configure();SessionFactory factory = configure.buildSessionFactory();Session session = factory.openSession();session.beginTransaction();//添加数据(在添加数据之前先——生成表,把添加数据部分先注释)Student stu = new Student(1, "aa", "nn", 25);ArrayList<String> hobby = new ArrayList<String>();hobby.add("旅游");hobby.add("散步");hobby.add("学习");hobby.add("跳舞");stu.setHobby(hobby);session.save(stu);/*//Map添加数据Student stu1 = new Student(2, "吖吖", "男", 26);stu.getHobby().put(1, "读书");stu1.getHobby().put(2, "旅游");stu2.getHobby().put(3, "跳舞");stu2.getHobby().put(4, "唱歌");session.save(stu);*/session.getTransaction().commit();;session.close();factory.close();}
}

转载于:https://www.cnblogs.com/jili6254/p/8268093.html

Hibernate Annotation _List/Map相关推荐

  1. Hibernate Annotation 学习

    1.一对多关联,级联操作 @OneToMany(mappedBy = "paymentad", cascade = CascadeType.ALL, fetch = FetchTy ...

  2. hibernate annotation注解方式来处理映射关系

    2019独角兽企业重金招聘Python工程师标准>>> 在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟 ...

  3. Hibernate Annotation的 *ToOne默认的FetchType是EAGER的

    Hibernate Annotation的 *ToOne默认的FetchType是EAGER的 public class Entry{ ... @ManyToOne(targetEntity = Us ...

  4. hibernate annotation多对多中间表添加其他字段的第三种方法

    本示例主要以学生(T_Student)和课程(T_Course)之间的多对多关系,中间表Score(分数),学生表和课程表是多对多关系,另外为他们的关系添加额外的字段---分数: T_Student类 ...

  5. Hibernate Annotation

    用Annotation 写ORM关联数据库表确实要简单很多(主要是可以省略一个配置文件),所以在Hibernate中都喜欢用Annotation 这个用例中有关于ID生成策略(所以用到了联合主键),和 ...

  6. Hibernate.Annotation注解

    Hibernate注解 1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="", ...

  7. Hibernate Annotation中英文文档链接下载 (Hibernate 注解)

    官网进入:http://www.hibernate.org 说明文档: 英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/ ...

  8. Hibernate annotation多对多配置

    角色(用户组),用户多对多. 角色实体配置: private Set<TAuthUser> users;@ManyToMany@JoinTable(name="t_auth_us ...

  9. hibernate annotation注解 columnDefinition用法

    column注解中的columnDefinition属性用于覆盖数据库DDL中的语句:(MySql) @Column(columnDefinition = "int(11) DEFAULT ...

最新文章

  1. LINUX samba的安装使用
  2. Spring Cloud【Finchley】-15 查看Zuul的路由端点和过滤器
  3. 用python编写杨辉三角金字塔_用python实现三道简单算法题:杨辉三角,蛇形矩阵,金字塔...
  4. 前端学习(902):this指向问题
  5. Codeup——问题 H: 部分A+B (15)
  6. 向mysql中插入时间_Java向mysql中插入时间的方法
  7. 这几天的学习进度总结
  8. 不用 SWIG,Go 使用 C++ 代码的方式
  9. mysql explain和profiling
  10. NLP之:百度SKEP
  11. python类属性定义_Python中类的定义与使用
  12. 另存为fdf或xps加载项_2007 Microsoft Office加载项:Microsoft另存为PDF或XPS
  13. 信息论与编码冯桂周林著答案_信息论与编码技术+(冯桂+林其伟+陈东华+著)+清华大学出版社+课后答案.pdf...
  14. Typora安装主题方法
  15. java覆盖写入_java写入文件(覆盖和续写)
  16. 蓝桥杯C/C++B组历届真题刷题【合集】
  17. Netty网络编程学习笔记(四)——进阶篇
  18. WORD打开很慢的解决办法[整理]
  19. win7设置定时开关机
  20. Mysql设置别名(表名和列名)

热门文章

  1. Python 技术篇-用PIL库修改图片透明度实例演示,改变png图片色道为RGBA、RGB
  2. CTFshow 命令执行 web44
  3. Maximum Product Subarray
  4. morphologyEx函数
  5. 聊天工具简单实现(python 半双工聊天)
  6. c语言实践 1/1+1/2+1/3+1/4+...+1/n
  7. 第四百一十四节,python常用算法学习
  8. 设计模式之装饰模式20170726
  9. 安装dos2unix
  10. 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)