Hibernate Annotation _List/Map
// 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的更多相关文章
- Hibernate Annotation笔记
(1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...
- hibernate annotation注解方式来处理映射关系
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...
- 使用hibernate annotation 为非空列加上默认值
在网上查了很多资料都没找到如何为非空列加上默认值 以前的做法是给字段一个初始值,加上dynamic-insert属性 换了annotation了以后没有找到如何设置dynamic-insert属性 但 ...
- Hibernate Annotation (Hibernate 注解)
简介: 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载. 然而现在借助新的 Hibernate Annotation 库, ...
- hello world of hibernate Annotation
1:建立所需要的类,如: package com.hibernate.model; import javax.persistence.Entity; import javax.persistence. ...
- hibernate annotation多对多中间表添加其他字段的第三种方法
本示例主要以学生(T_Student)和课程(T_Course)之间的多对多关系,中间表Score(分数),学生表和课程表是多对多关系,另外为他们的关系添加额外的字段---分数: T_Student类 ...
- hibernate annotation 生成uuid主键
JPA标准方式下,不可以生成uuid类型的主键,但是hibernate提供了一些方式生成uuid主键,具体如下: 1.主键生成器 @GeneratedValue(generator=" ...
- 第二个hibernate Annotation版本的helloworld
经过第一次的 hibernate 我发现每一个数据库表都对应了一个类,并且每一个类都要新建一个文件进行配置 很麻烦! 于是便出现了Annotation版本的hibernate. 具体如下: 1.同 ...
- hibernate annotation配置经验
1.将annotation写在entity类文件的get方法上面
随机推荐
- Linux下转换文件从GBK到UTF-8
iconv -f gbk -t utf- source-file -o target-file
- x&(x-1)
x&(x-1)可以用来求出x是否为2幂次方数:当&的结果为0时,x原值是2幂次方数,否则就不是2幂次方数: x=x&(x-1)即把x从低位开始的第一个1改成0.如1000,把1 ...
- Git分支创建与合并
分支管理是Git支持多人协作和版本控制的关键,参照廖雪峰对Git的介绍,对真实开发环境中Git的使用结合实践进行梳理. 摘自:廖雪峰的官方网站 在实际开发中,我们应该按照几个基本原则进行分支管理: 首 ...
- Django中的Python高级特性
本小文的内容实际是作为<Pro Django>第二版第二章的读书笔记简单总结. 1.类的构建:元类,使用带元类的基类----这个特性的案例主要就是models.Model类,用这种方式高效 ...
- 20岁少年小伙利用Python_SVM预测股票趋势月入十万!
在做数据预处理的时候,超额收益率是股票行业里的一个专有名词,指大于无风险投资的收益率,在我国无风险投资收益率即是银行定期存款. pycharm + anaconda3.6开发,涉及到的第三方库有p ...
- 凸包问题——Graham Scan
Graham Scan 概述: 对于凸多边形的定义不在这里做详细叙述,这里给出算法的实现原理. Step 1: 找出x值最小的点的集合,从其中找出y值最小的点作为初始点 Step 2: 获得新序列后, ...
- GitHub学习笔记:分支管理
GitHub对于每个开发版本都需要有一个分支,默认的分支是master往往被大家保留下来作为主分支,分支类似于进程的一个指针,往往在master这个稳定的主干版本上分出一个或多个正在开发的分支版本,开 ...
- 手把手教你如何安装Pycharm——靠谱的Pycharm安装详细教程
今天小编给大家分享如何在本机上下载和安装Pycharm,具体的教程如下: 1.首先去Pycharm官网,或者直接输入网址:http://www.jetbrains.com/pycharm/downlo ...
- Scrapy爬虫框架第四讲(Linux环境)
下面我们来学习Selector的具体使用:(参考文档:http://scrapy-chs.readthedocs.io/zh_CN/1.0/topics/selectors.html) Selecto ...
- 创建第一个core项目(netCore学习笔记1)
1.安装 core和netFramework其实是相对独立的,但是core的IDE是在vs2017才开始支持,而vs2017的安装环境必须搭配.net4.6,所以: Step1:安装.net4.6 S ...