//  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;
}
@Override
public 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();
}
}

Hibernate Annotation _List/Map的更多相关文章

  1. Hibernate Annotation笔记

    (1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...

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

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...

  3. 使用hibernate annotation 为非空列加上默认值

    在网上查了很多资料都没找到如何为非空列加上默认值 以前的做法是给字段一个初始值,加上dynamic-insert属性 换了annotation了以后没有找到如何设置dynamic-insert属性 但 ...

  4. Hibernate Annotation (Hibernate 注解)

    简介: 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载. 然而现在借助新的 Hibernate   Annotation 库, ...

  5. hello world of hibernate Annotation

    1:建立所需要的类,如: package com.hibernate.model; import javax.persistence.Entity; import javax.persistence. ...

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

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

  7. hibernate annotation 生成uuid主键

    JPA标准方式下,不可以生成uuid类型的主键,但是hibernate提供了一些方式生成uuid主键,具体如下: 1.主键生成器     @GeneratedValue(generator=" ...

  8. 第二个hibernate Annotation版本的helloworld

    经过第一次的 hibernate  我发现每一个数据库表都对应了一个类,并且每一个类都要新建一个文件进行配置 很麻烦!  于是便出现了Annotation版本的hibernate. 具体如下: 1.同 ...

  9. hibernate annotation配置经验

    1.将annotation写在entity类文件的get方法上面

随机推荐

  1. CALayer的属性和使用

    一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...

  2. Read程序员的困境有感

    看完这篇文章,我真的害怕了,码农遍地都是,我就是,改变从今天做起,不走码农生活 首先, 打造你自己的私人项目.你需要不断地打磨自己的技艺.如果工作本身并不能帮助你做到这一点,就捡起那些你感兴趣的问题, ...

  3. 一款C++写的tcp性能测试(压力测试)工具

    基线终于开发完了,自己写的服务器性能和压力承受能力到底怎么样,在网上没有找到合适的测试工具可以用.于是自己基于MFC写了一款测试工具. 启动界面如下: 设置参数后,运行如下: 运行完界面如下: 源码路 ...

  4. ubuntu11.04安装nginx+php+mysql

    先列参考内容,后面我再补充点东西: http://www.4wei.cn/archives/1001436 http://www.gidot.net/blog/article.asp?id=322 上 ...

  5. Ocelot中文文档-服务发现

    Ocelot允许您指定服务发现提供程序,并使用它来查找Ocelot正在将请求转发给下游服务的主机和端口.目前,这仅在GlobalConfiguration部分中受支持,这意味着所有ReRoute将使用 ...

  6. ThinkPHP5从零基础搭建CMS系统(二)

    接上节,开启wamp集成环境,在浏览器地址栏输入http://localhost/cms/public,即可运行项目,但是这边域名太长,做一下处理. 注:需要查看tp5全部教程,请点击右侧thinkp ...

  7. java之Spring(AOP)前奏-动态代理设计模式(下)

    在上一章我们看到了,新增的三种类都能实现对原始功能类进行添加功能的事务处理,这三种类就是一个代理. 但是这种代理是写死的,怎样实现对任意接口添加自定义的代理呢? 我们先来看一下之前的代理实现: pub ...

  8. Html5列表元素

    <ol> 有序列表: 属性:type(设置样式),reversed(倒序) <ul>无序列表 <li>表示列表中的项(在<ol>和<ul>中 ...

  9. IP路由及静态路由配置

    IP路由及静态路由配置 qianghaohao(CodingNutter) 链接来源:http://www.cnblogs.com/codingnutter/p/5654271.html 一.IP路由 ...

  10. Django+xadmin打造在线教育平台(九)

    目录 在线教育平台(一)      在线教育平台(二) 在线教育平台(三)      在线教育平台(四) 在线教育平台(五)      在线教育平台(六) 在线教育平台(七)      在线教育平台( ...