目的是不写xxx.hbm.xml映射文件,使用注解

主配置文件还是要有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:///hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- c3p0数据源 -->
<property name="c3p0.max_size">10</property>
<property name="c3p0.min_size">2</property>
<property name="c3p0.timeout">5000</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.validate">false</property> <property name="show_sql">true</property>
<property name="current_session_context_class">thread</property> <!-- 开启二级缓存 使用EhCache实现-->
<property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 不同的是这里,不写映射文件,而是完全限定名 -->
<mapping class="com.kaishengit.pojo.User"/>
<mapping class="com.kaishengit.pojo.Address"/>
<mapping class="com.kaishengit.pojo.Teacher"/>
<mapping class="com.kaishengit.pojo.Student"/>
<mapping class="com.kaishengit.pojo.Employee"/>
<mapping class="com.kaishengit.pojo.Dept"/>
<mapping class="com.kaishengit.pojo.Person"/>
<mapping class="com.kaishengit.pojo.Card"/>
<mapping class="com.kaishengit.pojo.Account"/>
<!--
<mapping resource="com/kaishengit/pojo/User.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Address.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Person.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Card.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Employee.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Dept.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Student.hbm.xml"/>
<mapping resource="com/kaishengit/pojo/Teacher.hbm.xml"/>
-->
</session-factory> </hibernate-configuration>

-------------------------------------------------------------------
-------------------------------------------------------------------

一对多,多对一 记得添加到主配置文件中去

取而代之的是在类上注解

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity// 表示这是一个pojo类
@Table(name="user")// 与数据库中表对应,表名和类名相同的可以不写
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)/*加入缓存 */
public class User { @Id/*放在这个属性上,表示这个属性在数据库中是主键,如果属性的名字和数据库中的
主键不一样,要加注解@column(name="id")*/
//@column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)/* 主键生成策略*/
private Integer id; /*普通属性不用管,自动映射,但是如果列名和属性名不一样要加column */
//@Column(name="username")
private String username;
private String password;
/* 多的一方维护关系,user不用管,mappedBy="user"(user是自己在对方类中属性的名字)相当于inverse,放弃关系维护 */
@OneToMany(mappedBy="user")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)// 加入缓存
@OrderBy("id desc")//排序
private Set<Address> addressSet;
}
@Entity
@Table
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)/* 加入缓存 */
public class Address { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String address;
@ManyToOne//多对一
@JoinColumn(name="userid")//指定外键
private User user;
}

---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------

多对多 ,记得添加到主配置文件中去

@Entity
@Table
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Student { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name; @ManyToMany /*自己在关系表中的外键*/ /* 对方在关系表中的外键*/
@JoinTable(name="student_teacher",joinColumns={@JoinColumn(name="sid")},inverseJoinColumns={@JoinColumn(name="tid")})
private Set<Teacher> teachers; }
@Entity
@Table
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Teacher { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToMany(mappedBy="teachers")//放弃维护
@OrderBy("id desc")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
private Set<Student> students; }

--------------------------------------------------------
--------------------------------------------------------

一对一

第一种一对一

person和card,card的id即作为主键,又作为外键

@Entity
@Table
public class Person { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name; @OneToOne
@PrimaryKeyJoinColumn/*告诉card,主键是靠着我(person) 产生的 */
private Card card;
@Entity
@Table
public class Card {
/* JPA中的主键生成策略只有四种,不支持外键生成器 所以写一个生成器GenericGenerator,名字叫FK
生成策略叫foreign,
这个person是属性,指明用card这个类的person属性对应的表的主键作外键*/
@Id
@GenericGenerator(name="FK",strategy="foreign",parameters={@Parameter(name="property",value="person")})
@GeneratedValue(generator="FK")// 指定多用的生成器
private Integer id;
private String cardnum; @OneToOne(mappedBy="card")
@PrimaryKeyJoinColumn
private Person person;

第二种一对一,是一对多或者多对一的特殊情况

dept和Employee,dept中有eid,Employee中有deptid

@Entity
@Table
public class Employee { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name="deptid")
private Dept dept;
}
@Entity
@Table
public class Dept { @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name="eid")
private Employee employee; }

----------------------------------------------------------
-------------------------------------------------------
-----------------------------------------------------

id不想用自动增长型,想用UUID怎么设置主键生成策略?

@Entity
@Table
public class Account { private String id;
private String name; @Id
@GenericGenerator(name="myuuid",strategy="uuid")
@GeneratedValue(generator="myuuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} // 排除不进行持久化操作的属性
@Transient
public String getNickName() {
return "Hello,Jack";
} }

程序执行.不用生成UUID,只需要保存

session.beginTransaction();

        Account account = new Account();
account.setName("hello"); session.save(account); session.getTransaction().commit();

hibernate Annotation 以及注解版的数据关联 4.4的更多相关文章

  1. hibernate Annotation 以及注解版的数据关联

    目的是不写xxx.hbm.xml映射文件,使用注解 主配置文件还是要有hibernate.cfg.xml <?xml version="1.0" encoding=" ...

  2. Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解

    参考: http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/ 1.系统配置: 可以通过使用  map ...

  3. hibernate annotation 之 注解声明

    @Entity 将一个 POJO 类注解成一个实体 bean ( 持久化 POJO 类 ) @Table 为实体 bean 映射指定具体的表,如果该注解没有被声明,系统将使用默认值 ( 即实体 bea ...

  4. 转Hibernate Annotation mappedBy注解理解

    在Annotation 中有这么一个@mappedBy 属性注解,相信有些同学还是对这个属性有些迷惑,上网找了些理解@mappedBy比较深刻的资料,下面贴出来供大家参考. http://xiaoru ...

  5. Rhythmk 学习 Hibernate 08 - Hibernate annotation 关联关系注解

    1.一对一 (One to One)    共三种情况:     1.1 主键共享    1.2 外键共享 1.3 中间表关联 1.1  code: @Entity public class arti ...

  6. Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)

    本文知识点(目录): 1.Annotation 注解版(只是测试建表)    2.XML版 的实现(只是测试建表)    3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...

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

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

  8. struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

  9. Hibernate Annotation (Hibernate 注解)

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

随机推荐

  1. MySql事务无法回滚的原因

    使用MySQL时.假设发现事务无法回滚,但Hibernate.Spring.JDBC等配置又没有明显问题时.不要苦恼,先看看MySQL创建的表有没有问题.即表的类型. InnoDB和MyISAM是在使 ...

  2. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...

  3. Ext2文件系统布局,文件数据块寻址,VFS虚拟文件系统

    注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...

  4. 借助树的概率dp(期望)+数学-好题-hdu-4035-Maze

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4035 题目意思: 有n个房间,有n-1条通道连接这n个房间(每两个房间之间有且只有一条路,所以实际上 ...

  5. j详细说明ava于clone办法

    原文地址:http://leihuang.org/2014/11/14/java-clone/ In java, it essentially means the ability to create ...

  6. linux进程之fork 和 exec函数

    ---恢复内容开始--- fork函数 该函数是unix中派生新进程的唯一方法. #include <unistd.h> pid_t   fork(void); 返回: (调用它一次, 它 ...

  7. 各种输入速度比较(cin,scanf,getchar,pascal的read)

    每个程序都是纯读入与1个输出.输入数据分别有1e2,1e3,1e4,1e5,1e6,1e7个数. C++使用getchar()读入是最快的.流输入是非常非常慢! 比赛还是用gechar比较好...

  8. 简单的UIScrollView 下拉刷新

    这里只贴主要代码 #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate&g ...

  9. UVA 10325 lottery 容斥原理

    题目链接 给出m个数, 求1-n的范围内, 无法整除这m个数之中任何一个数的数的个数. 设m个数为a[i], 对任意的i, n/a[i]是n中可以整除a[i]的数的个数, 但是这样对于有些数重复计算了 ...

  10. 0527 python 基础01

    折行的处理 \>>> print "hi \... hello Lucy!"hi hello Lucy! 自然字符串,字符串加上r或R前缀指定>>&g ...